Cleanup after all junit tests

I recommend to use org.junit.runner.notification.RunListener, example: public class TestListener extends RunListener { @Override public void testRunStarted(Description description) throws Exception { // Called before any tests have been run. } @Override public void testRunFinished(Result result) throws Exception { // Called when all tests have finished } } Read more directly in JUnit java doc. You can … Read more

differences between 2 JUnit Assert classes

The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase. That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way. Since version 4 of JUnit, the framework uses Annotations for marking tests. So you no longer need to extend TestCase. But that means, … Read more

How to write JUnit test with Spring Autowire?

Make sure you have imported the correct package. If I remeber correctly there are two different packages for Autowiring. Should be :org.springframework.beans.factory.annotation.Autowired; Also this looks wierd to me : @ContextConfiguration(“classpath*:conf/components.xml”) Here is an example that works fine for me : @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { “/applicationContext_mock.xml” }) public class OwnerIntegrationTest { @Autowired OwnerService ownerService; @Before public … Read more

Is Java’s assertEquals method reliable?

You should always use .equals() when comparing Strings in Java. JUnit calls the .equals() method to determine equality in the method assertEquals(Object o1, Object o2). So, you are definitely safe using assertEquals(string1, string2). (Because Strings are Objects) Here is a link to a great Stackoverflow question regarding some of the differences between == and .equals().

How do I assert an Iterable contains elements with a certain property?

Thank you @Razvan who pointed me in the right direction. I was able to get it in one line and I successfully hunted down the imports for Hamcrest 1.3. the imports: import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.contains; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.beans.HasPropertyWithValue.hasProperty; the code: assertThat( myClass.getMyItems(), contains( hasProperty(“name”, is(“foo”)), hasProperty(“name”, is(“bar”)) ));

How does Junit @Rule work?

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 to use @Before and @After. Using an ExternalResource rather than @Before and @After gives opportunities for better code reuse; the … Read more

Mockito: Mock private field initialization

Mockito comes with a helper class to save you some reflection boiler plate code: import org.mockito.internal.util.reflection.Whitebox; //… @Mock private Person mockedPerson; private Test underTest; // … @Test public void testMethod() { Whitebox.setInternalState(underTest, “person”, mockedPerson); // … } Update: Unfortunately the mockito team decided to remove the class in Mockito 2. So you are back to … Read more

How to run all tests belonging to a certain Category in JUnit 4

I found out one possible way to achieve what I want, but I don’t consider this to be the best possible solution as it relies on ClassPathSuite library that is not part of JUnit. I define the test suite for slow tests like this: @RunWith(Categories.class) @Categories.IncludeCategory(SlowTests.class) @Suite.SuiteClasses( { AllTests.class }) public class SlowTestSuite { } … Read more