摘要:
使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring Boot整合了Junit测试,对测试目标进行单元测试又方便了一步。
TestController:
package com.edu.usts.controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Spring Boot整合测试
*/
@EnableAutoConfiguration
@Controller
public class TestController {
@RequestMapping("test")
@ResponseBody
public String test(){
return "test springboot";
}
public static void main(String[] args) {
SpringApplication.run(TestController.class,args);
}
}
这边返回测试值为 “test springboot”,后面我们使用TestCase.assertEquals(),对返回值和期望值之间进行比较。
正确案例:
package com.edu.usts.test;
import com.edu.usts.controller.TestController;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.security.RunAs;
/**
* Spring boot 整合测试
*/
@SpringBootTest(classes = TestController.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestSpringController {
@Autowired
private TestController testController;
@Test
public void test(){
TestCase.assertEquals(this.testController.test(),"test springboot");
}
}
运行截图:
绿色表示值对,则运行成功。我们修改测试值。
错误案例:
package com.edu.usts.test;
import com.edu.usts.controller.TestController;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.security.RunAs;
/**
* Spring boot 整合测试
*/
@SpringBootTest(classes = TestController.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestSpringController {
@Autowired
private TestController testController;
@Test
public void test(){
TestCase.assertEquals(this.testController.test(),"test");
}
}
运行截图:
返回显示目标值和期望值不同,则提示错误。
项目截图:
忽略某些Controller,仅看Controller:TestController.java和测试:TestSpringController.java
pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>study_sb</groupId>
<artifactId>study_sb</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 定义公共资源版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!--控制JDK版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
源码gitee地址: