你的位置:首页 > Java教程

[Java教程]INTEGRATION TESTING WITH SPRING AND JUNIT


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

ProductServiceTestConfig

 

 

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.

 1 package guru.springframework.test.config; 2  3 import guru.springframework.repositories.ProductRepository; 4 import guru.springframework.repositories.ProductRepositoryTestStub; 5 import guru.springframework.services.ProductService; 6 import guru.springframework.services.ProductServiceImpl; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Configuration; 9 10 @Configuration11 public class ProductServiceTestConfig {12   @Bean13   ProductRepository productRepository(){14     return new ProductRepositoryTestStub();15   }16 17   @Bean18   ProductService productService(){19     return new ProductServiceImpl();20   }21 }

 

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

ProductServiceImplIt

 

 

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.

 1 package guru.springframework.services; 2  3 import guru.springframework.domain.Product; 4 import guru.springframework.test.config.ProductServiceTestConfig; 5 import org.junit.Test; 6 import org.junit.runner.RunWith; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.test.context.ContextConfiguration; 9 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;10 11 import static org.junit.Assert.assertEquals;12 13 @RunWith(SpringJUnit4ClassRunner.class)14 @ContextConfiguration(classes = {ProductServiceTestConfig.class})15 public class ProductServiceImplIT {16   private ProductService productService;17 18   @Autowired19   public void setProductService(ProductService productService) {20     this.productService = productService;21   }22 23   @Test24   public void testGetProduct(){25     Product product = productService.getProduct(1L);26     assertEquals(product.getDescription(), "This is a test product");27   }28 }