spingboot2.x集成单元测试
# 一注解说明
@SpringBootTest注解是扫描配置来构建测试用的Spring上下文环境。
@RunWith注解声明测试是在spring环境下运行的,其必须要有一个构造器的类才能运行。
SpringRunner.class是继承SpringJUnit4ClassRunner,主要作用是spring集成junit单元测试类。
二 顶级类测试
2.1引入单元测试依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring 5.0 webflux-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
2.2 创建包结构
注意哦,在测试类的java目录下和main子目录的java目录下,其最顶级的启动类包结构应该相同,否则要加包扫描注解。
本次示例先演示没有启动类的情况。
2.3 创建顶级类
/**
* @Author lsc
* @Description <p> 顶级类</p>
* @Date 2019/10/20 18:14
* @Version 1.0
*/
public class TestConfig {
public String getUser(){
return "youku1327";
}
}
2.4 顶级类测试示例
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/10/20 17:36
* @Version 1.0
*/
@RunWith(SpringRunner.class)
// 如果没有main主类,是顶级类要指定classes
@SpringBootTest(classes = TestConfig.class)
public class Youku1Test {
@Test
public void test(){
TestConfig testConfig = new TestConfig();
String user = testConfig.getUser();
// youku1327
System.out.println(user);
}
}
三 MockMvc 测试
3.1 创建controller层类
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/10/20 18:53
* @Version 1.0
*/
@RestController
public class TestController {
@GetMapping("youku1327")
public String getUser(){
return "Hello World youku1327";
}
}
3.2 创建主启动类
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/10/20 18:45
* @Version 1.0
*/
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class,args);
}
}
3.3 MockMvc测试示例
/**
* @Author lsc
* @Description <p> web 测试 </p>
* @Date 2019/10/20 18:33
* @Version 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class WebTest {
@Autowired
private MockMvc mvc;
@Test
public void exampleTest() throws Exception {
// get请求 rui /youku1327 状态 200 期望值 Hello World youku1327
ResultActions resultActions = this.mvc.perform(get("/youku1327")).andExpect(status().isOk())
.andExpect(content().string("Hello World youku1327"));
}
}
四 webClient测试示例
在步骤三的基础上进行测试。
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/10/20 19:00
* @Version 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureWebTestClient
@WebFluxTest(TestController.class)
public class WebClientTest {
WebTestClient webClient = WebTestClient.bindToController(new TestController()).build();
/*@Autowired
private WebTestClient webClient;*/
@Test
public void exampleTest() {
EntityExchangeResult<String> entityExchangeResult = webClient.get().uri("/youku1327").exchange().expectStatus().isOk()
.expectBody(String.class).returnResult();
// Hello World youku1327
System.out.println(entityExchangeResult.getResponseBody());
}
}
五 WebClient随机端口测试示例
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/10/20 19:27
* @Version 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class webReadomTest {
@Autowired
private WebTestClient webClient;
@Test
public void exampleTest() {
EntityExchangeResult<String> entityExchangeResult = this.webClient.get()
.uri("/youku1327").exchange().expectStatus().isOk()
.expectBody(String.class).returnResult();
// Hello World youku1327
System.out.println(entityExchangeResult.getResponseBody());
}
}
六 TestRestTemplate测试示例
/**
* @Author lsc
* @Description <p> </p>
* @Date 2019/10/20 19:39
* @Version 1.0
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RestTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void exampleTest() {
String body = this.restTemplate.getForObject("/youku1327", String.class);
//Hello World youku1327
System.out.println(body);
}
}
七 更多测试
更多测试信息请查看官网指南
https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/html/
公众号更多技术干货和java学习资源。