Spring ConfigurationThere are number of different ways to create Spring Beans. In this example I’m going to use a Java configu ...
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.
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 }
原标题:INTEGRATION TESTING WITH SPRING AND JUNIT
关键词:Spring
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:
admin#shaoqun.com
(#换成@)。