Spring 测试

  测试是开发工作中必不可缺的部分。单元测试只针对当前开发的类和方法进行测试,可以简单通过模拟依赖来实现,对运行环境没有依赖;但是仅仅进行单元测试是不够的,它只能验证当前类或方法能否正常工作,而我们想要知道系统的各个部分组合一起是否能正常工作,这就是集成测试存在的意义。

  集成测试一般需要来自不同层的不同对象的交互,如数据库、网络连接、Ioc容器等。其实我们也经常通过运行程序,然后通过自己操作来完成类似于集成测试的流程。集成测试为我们提供了一种无须部署或运行程序来完成验证系统各部分是否能正常协作工作的能力。

  Spring通过Spring TestContext Framework对集成测试提供顶级支持。它不依赖于特定的测试框架,既可以使用Junit,也可以使用TestNG。

  基于Maven构建的项目结构默认有关测试的目录:src/test/java (测试代码)、src/test/resource(测试资源)、区别于src/main/java(项目资源)、src/main/resources(项目资源)。

  Spring提供了一个SpringJunit4ClassRunner类,它提供了Spring TestContext Framework的功能。通过@ContextConfigutaion来配置Application Context,通过@ActiveProfiles确定活动的profile。

  在使用了Spring测试后,我们前面的例子的“运行”部分都儿科与你用Spring测试来检验功能能否正常运作。


示例:

(1)准备

增加Spring测试的依赖包到Maven:

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>

(2)业务代码

在src/main/java下的源码:

package com.lwh.highlight_spring4.ch3.fortest;

/**
 * Created by luwenhu on 2017/9/21.
 */
//测试bean
public class TestBean {
    private String content;

    public TestBean(String content){
        super();
        this.content = content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }
}

(3)配置类

在src/main/java下的源码:

package com.lwh.highlight_spring4.ch3.fortest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * Created by luwenhu on 2017/9/21.
 */
//配置类
@Configuration
public class TestConfig {

    @Bean
    @Profile("dev")
    public TestBean devTestBean(){
        return new TestBean("from development profile") ;
    }

    @Bean
    @Profile("prod")
    public TestBean prodTestBean(){
        return new TestBean("from production profile");
    }
}

(4)测试

在src/test/java下源码:

import com.lwh.highlight_spring4.ch3.fortest.TestBean;
import com.lwh.highlight_spring4.ch3.fortest.TestConfig;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by luwenhu on 2017/9/21.
 */
@RunWith(SpringJUnit4ClassRunner.class)//在JUnit环境下提供Spring TestContext Framework的功能
@ContextConfiguration(classes = {TestConfig.class})//@ContextConfiguration用来加载配置ApplicationContext,其中classes属性用来加载配置类
@ActiveProfiles("dev")//用来声明活动的profile
public class DemoBeanIntegrationTests {
    @Autowired//注入bean
    private TestBean testBean;

    @Test
    public void devBeanShouldInject(){
        String expected = "from development profile";
        String actual = testBean.getContent();
        Assert.assertEquals(expected,actual);
    }
}
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
原文地址:https://www.cnblogs.com/wenhulu/p/7567492.html