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

How to define a JUnit method rule in a test suite?

This can be done, but it needs a bit of work. You need to define your own Suite runner and your own Test runner as well, and then override runChild() in the test runner. Using the following: AllTests.java: @RunWith(MySuite.class) @SuiteClasses({Class1Test.class}) public class AllTests { } Class1Test.java: public class Class1Test { @Deprecated @Test public void test1() … Read more

Reuse spring application context across junit test classes

Yes, this is perfectly possible. All you have to do is to use the same locations attribute in your test classes: @ContextConfiguration(locations = “classpath:test-context.xml”) Spring caches application contexts by locations attribute so if the same locations appears for the second time, Spring uses the same context rather than creating a new one. I wrote an … 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().

Disable @EnableScheduling on Spring Tests

If you don’t want to use profiles, you can add flag that will enable/disable scheduling for the application In your AppConfiguration add this @ConditionalOnProperty( value = “app.scheduling.enable”, havingValue = “true”, matchIfMissing = true ) @Configuration @EnableScheduling public static class SchedulingConfiguration { } and in your test just add this annotation to disable scheduling @TestPropertySource(properties = … Read more

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