junit4

JUNIT Test class in Eclipse - java.lang.ClassNotFoundException

淺唱寂寞╮ 提交于 2019-12-17 23:26:08
问题 I'm trying to run my junit test (to verify that a properties file loads correctly) but I get ClassNotFoundException although the class is there and all required libraries are there too. Here it is the error I get : Class not found ConfigurationManagerTest java.lang.ClassNotFoundException: ConfigurationManagerTest at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

Why is assertEquals(double,double) deprecated in JUnit?

[亡魂溺海] 提交于 2019-12-17 17:53:15
问题 I was wondering why assertEquals(double, double) is deprecated. I used import static org.junit.Assert.assertEquals; and I used JUnit 4.11. Below is my code: import org.junit.Test; import static org.junit.Assert.assertEquals; public class AccountTest { @Test public void test() { Account checking = new Account(Account.CHECKING); checking.deposit(1000.0); checking.withdraw(100.0); assertEquals(900.0, checking.getBalance()); } } checking.getBalance() returns a double value. What could be wrong?

Cleanup after all junit tests

佐手、 提交于 2019-12-17 10:43:13
问题 In my project I have to do some repository setup before all tests. This is done using some tricky static rules. However I've got no clue how to do clean up after all the tests. I don't want to keep some magic static number referring the number of all test methods, which I should maintain all the time. The most appreciated way is to add some listener which would be invoked after all the tests. Is there any interface for it already in JUnit4? edit: this has nothing to do with @BeforeClass and

How to run all JUnit tests in a category/suite with Ant?

[亡魂溺海] 提交于 2019-12-17 09:33:03
问题 I'm using JUnit Categories and ClassPathSuite in a setup similar to that described in this answer. To recap: public interface FastTests { } @RunWith(Categories.class) @Categories.IncludeCategory(FastTests.class) @Suite.SuiteClasses(AllTests.class) public class FastTestSuite { } @RunWith(ClasspathSuite.class) public class AllTests { } ...where AllTests makes use of the ClasspathSuite library. A test class that's part of the FastTests category would look like this: @Category(FastTests.class)

How to run all JUnit tests in a category/suite with Ant?

﹥>﹥吖頭↗ 提交于 2019-12-17 09:32:35
问题 I'm using JUnit Categories and ClassPathSuite in a setup similar to that described in this answer. To recap: public interface FastTests { } @RunWith(Categories.class) @Categories.IncludeCategory(FastTests.class) @Suite.SuiteClasses(AllTests.class) public class FastTestSuite { } @RunWith(ClasspathSuite.class) public class AllTests { } ...where AllTests makes use of the ClasspathSuite library. A test class that's part of the FastTests category would look like this: @Category(FastTests.class)

How does Junit @Rule work?

只谈情不闲聊 提交于 2019-12-17 07:00:10
问题 I want to write test cases for a bulk of code, I would like to know details of JUnit @Rule annotation feature, so that I can use it for writing test cases. Please provide some good answers or links, which give detailed description of its functionality through a simple example. 回答1: Rules are used to add additional functionality which applies to all tests within a test class, but in a more generic way. For instance, ExternalResource executes code before and after a test method, without having

Mockito: Mock private field initialization

假如想象 提交于 2019-12-17 06:29:08
问题 How I can mock a field variable which is being initialized inline? e.g. class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock person.someMethod() while testing method - Test#testMethod. for which I need to mock initialization of person variable. Any clue? EDIT: I'm not allowed to modify Person class. 回答1: Mockito comes with a helper class to save you some reflection boiler plate code: import org.mockito.internal.util

Reuse spring application context across junit test classes

家住魔仙堡 提交于 2019-12-17 03:24:10
问题 We've a bunch of JUnit test cases (Integration tests) and they are logically grouped into different test classes. We are able to load Spring application context once per test class and re-use it for all test cases in a JUnit test class as mentioned in http://static.springsource.org/spring/docs/current/spring-framework-reference/html/testing.html However, we were just wondering if there is a way to load Spring application context only once for a bunch of JUnit test classes. FWIW, we use Spring

@RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)

旧城冷巷雨未停 提交于 2019-12-17 02:07:09
问题 While writing a new jUnit4 test, I'm wondering whether to use @RunWith(MockitoJUnitRunner.class) or MockitoAnnotations.initMocks(this) . I created a new test & the wizard automatically generated a test with the Runner. Javadocs for MockitoJUnitRunner state the following: Compatible with JUnit 4.4 and higher, this runner adds following behavior: Initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary. Mocks are initialized before

JUnit test if else case

落爺英雄遲暮 提交于 2019-12-14 03:59:16
问题 How to write test to current method? I use jUnit 4. public void setImage() { if(conditionOne){ myView.setImageOne(); } else { myView.setImageTwo(); } } 回答1: You need to write two tests to cover both the scenarios as below: import org.junit.Test; public class SetImageTest { @Test public void testSetImageForConditionOne() { //write test to make conditionOne true } @Test public void testSetImageForElseCondition() { //write test to make conditionOne false } } 回答2: Okay... there is a flaw in the