版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangminemail/article/details/82930233
- 添加测试依赖
Spring-boot-starter-test
Scope:test
- 添加测试注解
@Runwith(SrpingRunner.class)
@SpringBootTest
- 在测试环境单独装配bean
使用@SpringBootTest(classes=xx.class)加载
加上测试配置类需注解@TestConfiguration
而使用@Configuration是无效的
- 环境Evn的测试
直接使用Enviroment接口可以自动装配,获取属性值
优先取测试环境的配置文件
- 给配置加配置项适用于测试类
方法一:使用注解@SpringBootTest的属性properties=”app.version=1”
方法二:使用@before接口注解,在接口中添加配置项
- Mapper的测试
使用注解@MockBean注入Mapper实例,使用BDDMockito这个类模拟
抛异常预测:
- 控制器Controller测试
依赖类TestRestTemplate工具测试控制器
@SpringBooTest加载Spring整个容器
测试一个带参数的Controller
Get方式的参数
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class BookRestTest {
@Autowired
private TestRestTemplate testRest;
@Test
public void testHome() {
String body = testRest.getForObject("/book/home", String.class);
Assert.assertEquals("book home", body);
}
@Test
public void testShow() {
String body = testRest.getForObject("/book/show?id=100", String.class);
Assert.assertEquals("book100", body);
}
}
- 控制器Controller测试,方式二,一下方法不会加载Spring容器
使用测试类注解@WebMvcTest(controllers=xxController.class) 通过属性指定控制器
使用MockMvc接口模拟测试Controller
@RunWith(SpringRunner.class)
@WebMvcTest(controllers=BookRest.class)
public class BookRestTest2 {
@Autowired
private MockMvc mvc;
@Test
public void testHome() throws Exception {
// String body = testRest.getForObject("/book/home", String.class);
// Assert.assertEquals("book home", body);
mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void testShow() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());
}
}
总结:
多个参数测试:
该种方法不会加载Spring整个容器,只会加载测试对象;
@SpringBooTest加载Spring整个容器
3、两者结合使用:使用注解@SpringBootTest加载整个容器bean,加上@AutoConfigureMockMvc注解开启MockMvc对象
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class BookRestTest3 {
@Autowired
private TestRestTemplate testRest;
@Autowired
private MockMvc mvc;
@Test
public void testHome() throws Exception {
String body = testRest.getForObject("/book/home", String.class);
Assert.assertEquals("book home", body);
// mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
}
@Test
public void testShow() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());
}
}