There is number of different ways to create Spring BeanS. In the 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 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 was easily overridden by passing a name to the bean annotation as @Bean(name = "Custombeanname"). In the This configuration class, I ' m defining II spring beans. The same test stub bean we used in the previous unit test example and the product service implementation. Notice that I don't manage the injection of the repository bean into the service bean. I allow the Spring Framework to manage the dependency injection.
1 PackageGuru.springframework.test.config;2 3 Importguru.springframework.repositories.ProductRepository;4 Importguru.springframework.repositories.ProductRepositoryTestStub;5 ImportGuru.springframework.services.ProductService;6 ImportGuru.springframework.services.ProductServiceImpl;7 ImportOrg.springframework.context.annotation.Bean;8 Importorg.springframework.context.annotation.Configuration;9 Ten @Configuration One Public classProductservicetestconfig { A @Bean - productrepository productrepository () { - return Newproductrepositoryteststub (); the } - - @Bean - Productservice Productservice () { + return NewProductserviceimpl (); - } +}
- Spring and Junit Configuration
To support Spring and JUnit, we need to add the new annotations to our test class. The first annotation is@Runwith(Springjunit4classrunner.Class) . The annotation is provided by the JUnit team, which are 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 classSpringjunit4classrunner.Class, this is the Test runner class supplied by the Spring Framework. This custom test runner was what enables the Spring Context. Your test class in effect becomes a spring Bean, and was managed in the spring context. The second annotation we need to use are@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 would your configuration. For we example today, our needs is not very complex. I only has 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 II annotations, when I run the JUnit test, the Spring Context would be started and the beans we ' V E specified in the configuration would be available for use with our test.
By convention, I ' m naming my integration Test with the suffix of ' IT '. Traditionally, you'll name your unit tests with the suffix of ' Test ' or ' tests ', and your integration tests with the SUF Fix of ' IT '. This does isn't affect how JUnit runs the tests. But it does has ramifications later when building with tools such as Maven and deploying and reporting from continuous Bu ILD 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 the SPR ing 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 beans into the test. This bean was a spring bean, which is configured and managed by Spring. Thus it would have the repository injected for us as we specified in our test configuration class.
1 Packageguru.springframework.services;2 3 Importguru.springframework.domain.Product;4 ImportGuru.springframework.test.config.ProductServiceTestConfig;5 Importorg.junit.Test;6 ImportOrg.junit.runner.RunWith;7 Importorg.springframework.beans.factory.annotation.Autowired;8 Importorg.springframework.test.context.ContextConfiguration;9 ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;Ten One Import Staticorg.junit.Assert.assertEquals; A -@RunWith (Springjunit4classrunner.class) -@ContextConfiguration (classes = {Productservicetestconfig.class}) the Public classProductserviceimplit { - PrivateProductservice Productservice; - - @Autowired + Public voidSetproductservice (Productservice productservice) { - This. Productservice =Productservice; + } A at @Test - Public voidtestgetproduct () { -Product Product = Productservice.getproduct (1L); -Assertequals (Product.getdescription (), "This is a test product"); - } -}
Integration testing with SPRING and JUNIT