本文用例基于Spring Boot+Kotlin实现。测试工程见:https://github.net/icarusliu/learn
1 常见测试场景
1.1 MVC测试单个Controller
可通过MockMvc进行特定MVC的请求测试处理;
要进行单个Controller的测试,需要使用@WebMvcTest来指定需要测试的Controller。
WebMvcTest及MockMvc的使用分析见后文。
import com.liuqi.learn.learninterceptor.controller.TestController
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.*
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@RunWith(SpringRunner::class)
@WebMvcTest(TestController::class)
class LearninterceptorApplicationTests {
private val logger = LoggerFactory.getLogger(LearninterceptorApplicationTests::class.java)
@Autowired
private lateinit var mvc: MockMvc
@Test
fun testTestController() {
mvc.perform(get("/test/test")).andExpect(status().isOk)
.andExpect(content().string("test"));
}
}
1.2 MVC测试多个Controller
可以针对多个Controller进行测试,启动的是同一个容器;
不需要指定待测试的Controller;
也可以通过MockMvc来启动模拟请求;
示例如下:
@RunWith(SpringRunner::class)
@SpringBootTest
@AutoConfigureMockMvc
class TestLearninterceptorApplication {
@Autowired
private lateinit var mvc: MockMvc
@Test
fun testTest() {
mvc.perform(MockMvcRequestBuilders.get("/test/test")).andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.content().string("test"));
}
@Test
fun testTest1() {
mvc.perform(MockMvcRequestBuilders.get("/test2/test")).andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.content().string("test2"));
}
}
1.3 测试打桩
可以通过MockBean进行测试打桩;
先看Service的定义:
@Service
class TestService {
fun test(): String {
return "test"
}
}
再看使用Service的Controller
@RestController
@RequestMapping("/test2")
class TestController2 {
val logger = LoggerFactory.getLogger(TestController2::class.java)
@Autowired
private lateinit var testService: TestService
@RequestMapping("/test")
fun test(): String {
logger.info("TestController2!")
return "test2"
}
@RequestMapping("/service")
fun testService(): String {
return testService.test()
}
}
最后编写测试类:
@SpringBootTest
@AutoConfigureMockMvc
class TestLearninterceptorApplication {
@Autowired
private lateinit var mvc: MockMvc
@MockBean
private lateinit var testService: TestService
@Test
fun testMock() {
BDDMockito.given(this.testService.test()).willReturn("mockTest")
mvc.perform(MockMvcRequestBuilders.get("/test/service")).andExpect(MockMvcResultMatchers.status().isOk)
.andExpect(MockMvcResultMatchers.content().string("mockTest"))
}
}
1.4 集成测试
可以通过TestRestTemplate来进行集成测试;即在应用已经部署并在测试环境启动时,可以通过指定URL的方式进行测试用例的测试;
一般通过TestRestTemplate来进行测试(当然TestRestTemplate也可以进行单元测试,后文会进行详细说明);
import org.hamcrest.CoreMatchers.*
import org.junit.Assert
import org.junit.Test
import org.springframework.boot.test.web.client.TestRestTemplate
class SITest {
private val restTemplate: TestRestTemplate = TestRestTemplate()
@Test
fun testRest() {
val body = restTemplate.getForEntity("http://localhost/test2/service", String::class.java).body
Assert.assertThat(body, `is`("test"))
}
}
2 常见测试类
2.1 WebMvcTest
与SpringRunner一起使用,可测试指定的Controller;
注意在使用时需要指定待测试的Controller,可以指定多个待测试的Controller;
@WebMvcTest(*[TestController::class, TestController2::class])
WebMvcTest一般用于单元测试中测试Controller及其生效的Filter等场景;他会启动一个Spring容器,但只会加载其中部分MVC相关的内容,如Component、Service或者Repository等注解的Bean不会进行加载。
会加载的:Controller/ControllerAdvice/JsonComponent/Converter/Filter/WebMvcConfigurer/HandlerMethodArgumentResolver等; 因此如果待测试的Controller中使用了如Service等内容,需要打桩处理。
它所启动的Spring容器将会随测试用例的开始执行而启动,随所有测试用例执行完毕而停止;因此它适合本地开发环境的测试用例执行。
2.2 SpringBootTest
与SpringRunner一起使用,不需要指定Controller进行测试。
SpringBootTest会启动一个完整的Spring容器,这意味着它会加载所有Spring的内容;类似于将应用部署到了测试环境的Tomcat中;
与WebMvcTest一样,启动的只是一个临时的环境,随着测试用例的执行而启动,在所有测试用例执行完毕后这个环境会将会停止, 适合本地开发环境的测试用例执行。
2.3 TestRestTemplate
TestRestTemplate可使用在以下两个场景的测试:
2.3.1 远程测试
TestRestTemplate可用于Rest服务的远程测试;针对的是应用已经部署在测试环境应用服务器中的场景;
它可通过指定http://ip:port/…这种路径的方式来访问特定链接进行集成测试;
2.3.1 本地测试
TestRestTemplate也可用于本地开发环境的测试,与MockMvc类似,可通过指定相对路径的方式调用本地启动的SpringMVC来进行MVC测试;这种情况下也需要与SpringBootTest集成使用。
它与MockMVC不一样的是,MockMVC只能针对返回的内容进行测试,而TestRestTemplate可对Body、Headers等进行测试;
使用方式如下:
@RunWith(SpringRunner::class)
@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT)
class TestLearninterceptorApplication2 {
@Autowired
private lateinit var template: TestRestTemplate
@Test
fun test() {
val body = template.getForEntity("/test2/service", String::class.java).body
Assert.assertThat(body, CoreMatchers.`is`("test"))
}
// @TestConfiguration
// class Config {
// @Bean
// fun restTemplateBuilder(): RestTemplateBuilder {
// return RestTemplateBuilder()
// }
// }
}
注意如果在SpringBootTest中不指定端口,则会报TestRestTemplate无法注入的错误。
TestRestTemplate的常用方法介绍如下:
方法名 | 说明 |
---|---|
getForObject | 通过Get方式请求并获取返回数据 |
getForEntity | 通过Get方式请求并获取返回对象,返回对象类型为ResponseEntity |
headForHeaders | 获取传入URL的Headers,返回对象为HttpHeaders |
postForLocation | 通过Post方发送请求,返回对象为URI |
postForObject | 通过POST方式请求并获取返回数据 |
postForEntity | 通过POST方式发送请求并获取返回对象,返回对象类型为ResponseEntity |
部分场景下会返回ResponseEntity对象,该对象的常用方法如下:
方法名 | 说明 |
---|---|
getStatusCode | 获取请求返回的状态码,如404等 |
getHeaders | 获取请求返回的Headers |
getBody | 获取请求返回的Body |
hasBody | 是否有返回Body |
2.4 MockMvc
MockMvc用于对本地启动的Spring环境进行测试,通过它的perform方法,它会模拟一个Http请求访问SpringMVC并返回ResultActions对象;
它一般要与SpringBootTest或者是WebMvcTest一起使用;
MockMvc的使用主要是使用其perform方法,这个方法返回类型是ResultActions对象;
这个对象包含以下方法:
方法 | 说明 | 使用 |
---|---|---|
andExpect | 判断结果是否满足某个ResultMatcher对象 | mockMvc.perform(post(“/form”)).andExpect(status().isOk()).andExpect(redirectedUrl(“/person/1”)) |
andDo | 针对结果做某种处理,如打印等; | mockMvc.perform(get(“/form”)).andDo(print()) |
andReturn | 返回MvcResult对象,可以获取Request、Response以及返回的数据等信息。 |
andExpect方法中的Matcher可以使用MockMvcResultMatchers来进行构建,其可构建的Matcher包括:
方法 | 说明 |
---|---|
request | 获取RequestResultMatchers类型的Matcher,可对Attribute、SessionAttribute等进行判断; |
handler | 获取HandlerResultMatchers类型的Matcher,具体用法见该类定义 |
model | 获取ModelResultMatchers类型的Matcher,可对返回的数据对象进行判断; |
view | 获取ViewResultMatchers类型的Matcher,可对返回视图的名称进行判断; |
forwardedUrl | 判断返回的页面链接是否符合预期 |
redirectedUrl | 判断跳转的页面链接是否符合预期 |
status | 返回StatusResultMatchers对象,判断请求状态是否符合预期 |
header | 返回HeaderResultMatchers对象,对返回的报文头内容进行判定 |
content | 返回ContentResultMatchers对象,对返回的Body内容进行判定 |
三者结构使用的示例见1.1章节
2.5 MockBean
MockBean用于打桩,一般与BDDMockito结合使用。
MockBean本身功能较简单,主要是BDDMockito的使用。
2.6 BDDMockito
详细请参考此文
用于替换被打桩的Bean中的指定方法;
其包含的常用方法如下:
2.6.1 given
替换被打桩的对象的指定方法;
使用方式如:
val result = BDDMockito.given(this.testService.test())
其返回的类型为:BDDMyOngoingStubbing,其包含有以下常用方法:
- willAnswer: 根据传入参数定制返回结果;
- will: 与willAnswer类似
- willReturn: 定制直接的返回结果;
- willThrow: 定制将会抛出的异常
使用方法如:
val result = BDDMockito.given(this.testService.test())
result.willReturn("test")
表示将这个方法的返回替换成test字符串。
2.6.2 then
请参考此文。
3 其它
3.1 WebMvcTest与SpringBootTest
这两个注解只能添加一个,如果混合使用会报错:
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class
其区别在于:
WebMvcTest用于单元测试场景,需要指定待测试的Controller;他也会启动一个Spring容器,但只会加载其中部分MVC相关的内容,如Component、Service或者Repository等注解的Bean不会进行加载。
会加载的:Controller/ControllerAdvice/JsonComponent/Converter/Filter/WebMvcConfigurer/HandlerMethodArgumentResolver等;
SpringBootTest:用于集成测试场景,不需要指定待测试的Controller;它会启动一个完整的Spring容器,所有的Bean都会进行加载与初始化。