【发布时间】:2015-05-21 04:07:23
【问题描述】:
我正在尝试测试应使用自定义错误属性的 Spring Boot RestController。
@Bean
public ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(
RequestAttributes requestAttributes,
boolean includeStackTrace) {
Map<String, Object> errorAttributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
Throwable error = getError(requestAttributes);
return errorAttributes;
}
};
}
但是当我尝试使用简单的测试来测试自定义错误属性时,这些属性没有被考虑在内。下面的测试实际上触发了一个请求,并且 i 除了使用了自定义属性。但无论我似乎做什么,代码似乎都没有被考虑在内。
class TestSpec extends Specification {
MockMvc mockMvc
def setup() {
mockMvc = MockMvcBuilders.standaloneSetup(new HelloWorldController()).build()
}
def "Test simple action"() {
when:
def response = mockMvc.perform(post("/hello")
.contentType(MediaType.APPLICATION_JSON)
.content('{"sayHelloTo": ""}')
)
then:
response.andExpect(status().isOk())
}
}
关于如何测试自定义属性是否有任何线索?
【问题讨论】:
标签: spring rest spring-boot