Integration Testing with Spring and JUnit

You will find a greater need for Integration tests as your testing requirements become more complex when you’re supporting enterprise application development using the Spring Framework. Sometimes, allowing Spring to manage the beans makes your testing easier, sometimes you want to test parts of your Spring Configuration, sometimes you’ll need to wire in a Mock component for your tests.

Spring and Junit are a powerful combination. You can easily bring in a custom Spring configuration to meet the needs of your test scenario.

Spring Configuration

There are number of different ways to create Spring Beans. In this example I’m going to use a Java configuration. Below is my Spring Configuration class for our integration test.


Note the use of the annotation @Configuration  at the top of the class. This designates the class as a Spring Configuration class. The annotation @Bean  tells Spring to use this method to load a Spring bean into the context. The default behavior of Spring is to use the name of the method as the bean name. This behavior is easily overridden by passing a name to the bean annotation as @Bean(name = "customBeanName"). In this configuration class, I’m defining two spring beans. The same test stub bean we used in the previous unit test example and the product service implementation. Notice that I do not manage the injection of the repository bean into the service bean. I allow the Spring Framework to manage the dependency injection.

Spring and JUnit Configuration

To support Spring and JUnit, we need to add two new annotations to our test class. The first annotation is @RunWith(SpringJUnit4ClassRunner.class) . The annotation is provided by the JUnit team, which is a API to allow for the extension of JUnit to allow for a customized test runner class. Within this annotation, we’re passing in the class SpringJUnit4ClassRunner.class , this is the test runner class supplied by the Spring Framework. This custom test runner is what enables the Spring Context. Your test class in effect becomes a Spring Bean, and is managed in the Spring context. The second annotation we need to use is @ContextConfiguration, this allows for us to specify the configuration for the Spring Context. The configuration of this annotation is very versatile. As the complexity of your application grows, so will your configuration. For our example today, our needs are not very complex. I only have one configuration class to bring into the Spring Context. I can do this simply by passing the class name to the annotation using the classes property like: @ContextConfiguration(classes = {ProductServiceTestConfig.class}). Through the use of these two annotations, when I run the JUnit test, the Spring Context will be started and the beans we’ve specified in the configuration will be available for use in our test.

JUnit Test

By convention, I’m naming my Integration Test with the suffix of ‘IT’. Traditionally, you will name your unit tests with the suffix of ‘Test’ or ‘Tests’, and your integration tests with the suffix of ‘IT’. This does not affect how JUnit runs the tests. But it does have ramifications later when building with tools such as Maven and deploying and reporting from Continuous Build servers such as Jenkins.


The class below is the same test we looked at above as a Unit test, but now it’s an Integration test, and we are using Spring to manage the dependency injection. Here we have the test class annotated with the annotations we just discussed above. Since the class is now managed by Spring, we can use the @Autowired annotation to inject our service bean into the test. This bean is a Spring Bean, which is configured and managed by Spring. Thus it will have the repository injected for us as we specified in our test configuration class.

Free Introduction to Spring Tutorial

Are you new to the Spring Framework? Checkout my Free Introduction to Spring Online Tutorial.

You May Also Like

4 comments on “Integration Testing with Spring and JUnit

  1. Can you tell me why this is different from a unit test of the method? You used a mock repository so where is the Integration? I thought it woukd involve using mock mvc to hit the controller to the service to the real repository bur with a mocked database using in memory or similsr? I’m confused as the project I’m on had these as unit tests and the IT tests fire against the running Web app. I’m quite new to spring so trying to read up.

  2. Its considered an integration test because I’m using the Spring context to manage an inject beans into the test class.

  3. Do you have more examples on JUNIT and Mockito ?

Leave a Reply