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 @Configuration
11 public class ProductServiceTestConfig {
12     @Bean
13     ProductRepository productRepository(){
14         return new ProductRepositoryTestStub();
15     }
16
17     @Bean
18     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     @Autowired
19     public void setProductService(ProductService productService) {
20         this.productService = productService;
21     }
22
23     @Test
24     public void testGetProduct(){
25         Product product = productService.getProduct(1L);
26         assertEquals(product.getDescription(), "This is a test product");
27     }
28 }
时间: 2024-10-09 19:06:08

INTEGRATION TESTING WITH SPRING AND JUNIT的相关文章

原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)

我用的是myeclipse 10,之前一直想要用junit来测试含有spring注解或动态注入的类方法,可是由于在网上找的相关的jar文件进行测试,老是报这样那样的错误,今天无意中发现myeclipse本就自带了 spring基于junit的测试框架,而且非常好用. 1.废话不多说,首先添加 测试框架的 类库:项目-->buildpath-->addlibraries-->myelipse libraries-->Spring 2.5 testing  support librar

Spring整合JUnit框架进行单元测试代码使用详解

[转]Spring整合JUnit框架进行单元测试代码使用详解 转自 http://blog.csdn.net/yaerfeng/article/details/25187775 感谢博主 :云淡风轻 .仅此一抹 一.Spring提供的JUnit框架扩展: 1. AbstractSpringContextTests:spring中使用spring上下文测试的Junit扩展类,我们一般不会使用这个类来进行单元测试,它是spring内部设计使用到的类    2. AbstractDependencyI

Spring与junit测试

1.我们知道无论在J2SE还是android中都有junit测试,利用junit能够帮助方便测试代码.在之前的博客中我也写了一些J2SE的一些junit测试例子,今天对于Spring中junit小小的讨论一下. 这个Spring测试需要的jar包:      spring-test-3.2.0.RELEASE.jar 2.Spring和Junit的关系图 左边的采用传统的方式,即一般的J2SE的方式测试代码,这种情况会有些问题: (1).每一个测试都要启动Spring, (2).这种情况下,是测

Struts2+Spring+Mybatis+Junit 测试

Struts2+Spring+Mybatis+Junit 测试 博客分类: HtmlUnit Junit Spring 测试 Mybatis Java代码   package com.action.kioskmonitor; /** * Junit群体测试Struts2 .spring.Mybatis */ import static org.junit.Assert.assertNotNull; import java.util.List; import java.util.UUID; imp

使用spring配合Junit进行单元测试的总结

最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注解指定使用springJunit的测试运行器,@ContextConfiguration注解指定测试用的spring配置文件的位置 之后我们就可以注入我们需要测试的bean进行测试,Junit在运行测试之前会先解析spring的配置文件,初始化spring中配置的bean @RunWith(Spri

spring中junit 提示Failed to load ApplicationContext

错误提示: 1:java.lang.IllegalStateException: Failed to load ApplicationContext 2:Error creating bean with name 'userService' defined in class path resource [UserService.xml]: Initialization of bean failed; nested exception is org.springframework.aop.fram

【Spring】Junit加载Spring容器作单元测试(转)

[Spring]Junit加载Spring容器作单元测试 阅读目录 > 基本的搭建 > 常见的用法 如果我们需要对我们的Service方法作单元测试,恰好又是用Spring作为IOC容器的,我们可以这么配置Junit加载Spring容器,方便做单元测试. > 基本的搭建 (1)引入所需的包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> &

Spring整合junit测试

本节内容: Spring整合junit测试的意义 Spring整合junit测试 一.Spring与整合junit测试的意义 在没整合junit之前,我们在写测试方法时,需要在每个方法中手动创建容器,获取对象,比如下面的代码,红色部分都是重复的代码.如果要测试很多功能的话,每次都得手动去创建容器,很麻烦.如果你测试的两个功能中用到某个相同的对象,获取对象的代码也得写一遍. public class test { @Test public void test1(){ //1.创建容器对象(创建Sp

Spring之junit测试集成

简介 Spring提供spring-test-5.2.1.RELEASE.jar 可以整合junit. 优势:可以简化测试代码(不需要手动创建上下文,即手动创建spring容器) 使用spring和junit集成的步骤 1.导入jar包 2.创建包com.igeek.test,创建类SpringTest 通过@RunWith注解,使用junit整合spring 通过@ContextConfiguration注解,指定spring容器的位置 3.通过@Autowired注解,注入需要测试的对象 在