Integration Testing with Spring and JUnit

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 a 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.

package guru.springframework.test.config;

import guru.springframework.repositories.ProductRepository;
import guru.springframework.repositories.ProductRepositoryTestStub;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class ProductServiceTestConfig {
    ProductRepository productRepository(){
        return new ProductRepositoryTestStub();

    ProductService productService(){
        return new ProductServiceImpl();


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 an 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 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.


import guru.springframework.domain.Product;
import guru.springframework.test.config.ProductServiceTestConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertEquals;

@ContextConfiguration(classes = {ProductServiceTestConfig.class})
public class ProductServiceImplIT {
    private ProductService productService;

    public void setProductService(ProductService productService) {
        this.productService = productService;

    public void testGetProduct(){
        Product product = productService.getProduct(1L);
        assertEquals(product.getDescription(), "This is a test product");


Free Introduction to Spring Tutorial

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


About jt

    You May Also Like