junit

How to verify that error was logged with unit tests

こ雲淡風輕ζ 提交于 2019-12-31 03:44:28
问题 Let's say I have the following class like this: public class MyClass { public static final Logger LOG = Logger.getLogger(MyClass.class); public void myMethod(String condition) { if (condition.equals("terrible")) { LOG.error("This is terrible!"); return; } //rest of logic in method } } My unit test for MyClass looks something like this: @Test public void testTerribleCase() throws ModuleException { myMethod("terrible"); //Log should contain "This is terrible!" or assert that error was logged }

How to print current executing JUnit test method while running all tests in a class or suite using ANT?

眉间皱痕 提交于 2019-12-31 02:28:07
问题 I have a set of JUnit Test Cases and I execute them from ANT using the junit task. While executing the tests, in the console I get to see only which test case (i.e. Java class) is currently running, but not the test method. Is there a way I can print the current executing test method? Or is there any other way to do this other than having my own JUnit test runner? Sample Console Output [junit] Running examples.TestCase1 [junit] Tests run: 3, Failures: 0, Errors: 0, Time elapsed: 2.835 sec

How to add JaCoCo in maven

若如初见. 提交于 2019-12-31 02:08:09
问题 I have written a unit test case using JUnit now I want to add JaCoCo in my build tool that is moving 3.2.1.I am new to Maven. While adding it, I have to doubt that I want to add it in the dependency or plugin ? There are both are available,such that is following <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.2-SNAPSHOT</version> </plugin> <dependency> <groupId>org.codehaus.sonar.plugins</groupId> <artifactId>sonar-jacoco-plugin</artifactId>

JUnit - stop it from exiting on finish?

孤人 提交于 2019-12-31 01:45:19
问题 Quick JUnit question. I'm running some unit tests that involve starting up the GUI and doing a load of stuff. I would like to see the results after the test to confirm it visually. However, it gets to the end of the code and exits, as it should. If I want to override this, I put a breakpoint on the last line of the test. This is pretty awkward though. Is there some option to stop it from exiting? 回答1: Due to the fact you require a GUI and user interaction during the execution of the test,

selenium RC例子

送分小仙女□ 提交于 2019-12-31 00:51:22
1、先启动selenium server,命令:java -jar selenium-server-standalone-2.44.0.jar 注:也可以把命令做成一个批处理文件,和selenium server放在同一级目录下,方便启动。 2、eclipse里面用junit框架验证selenium脚本的运行结果,如下面的例子: package autotest; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.thoughtworks.selenium.DefaultSelenium; import com.thoughtworks.selenium.Selenium; @SuppressWarnings("deprecation") public class test12 { private Selenium selenium; @Before public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*firefox C:/program Files (x86)

IDEA+maven项目结构+Junit测试

梦想与她 提交于 2019-12-30 23:35:11
目录结构 Build之后生成target目录: 一直报Mapper找不到方法的错误。发现是build Project生成的target下classes下的文件不全。删了重新Build之后就好了。 IDEA添加测试类 在需要测试的方法类里shift+Ctrl+T 然后选择Create New Test 在测试类上加两个注解 在pom.xml里 scope要是test,防止把test类也部署到tomcat下。这样生成的target下是没有test类的。 来源: CSDN 作者: Irisohohoh 链接: https://blog.csdn.net/Irisohohoh/article/details/103769987

jMockit: How to expect constructor calls to Mocked objects?

大憨熊 提交于 2019-12-30 21:04:39
问题 I am unit-testing a method performing some serialization operations. I intend to mock the serialization logic. The code is as below: ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); I have created the following mock objects: @Mocked FileInputStream mockFIS; @Mocked BufferedInputStream mockBIS; @Mocked ObjectInputStream mockOIS; I have setup a NonStrictExpectations() block where I want to expect the above constructor calls. Any ideas on how I

jMockit: How to expect constructor calls to Mocked objects?

旧时模样 提交于 2019-12-30 21:03:21
问题 I am unit-testing a method performing some serialization operations. I intend to mock the serialization logic. The code is as below: ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file))); I have created the following mock objects: @Mocked FileInputStream mockFIS; @Mocked BufferedInputStream mockBIS; @Mocked ObjectInputStream mockOIS; I have setup a NonStrictExpectations() block where I want to expect the above constructor calls. Any ideas on how I

Running only certain junit tests in a project by file name

心已入冬 提交于 2019-12-30 19:26:10
问题 So i have an eclipse project, with a bunch of junit test files. I want to run all of them, except ones that have a certain suffix (or inversely, only those that have one of a set of suffixes). Eg, i can do this in maven; i can run all tests in a project that are in files that end in "Test", and all other files are ignored. I want to do something similar in eclipse w/o having to configure things; just based on names. Right now i would go to "run configurations.." and then select "run all tests