Mar*_*rco 3 rest spring spring-boot
我正在尝试测试应该使用自定义错误属性的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;
}
};
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用简单测试来测试自定义错误属性时,不会考虑这些属性.下面的测试实际上会触发一个请求,除了使用自定义属性.但无论我做什么,代码似乎都没有被考虑在内.
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())
}
}
Run Code Online (Sandbox Code Playgroud)
关于我如何测试自定义属性的任何线索?
And*_*son 10
Spring Boot的错误基础结构通过将请求转发到错误控制器来工作.这是使用ErrorAttributes实例的错误控制器.MockMvc只对测试转发请求提供了相当基本的支持(您可以检查请求是否会被转发,但不能转发该转发的实际结果).这意味着,HellowWorldController使用独立安装程序或基于Web应用程序上下文的设置调用您的MockMvc测试不会驱动正确的代码路径.
一些选择:
ErrorAttributes直接单元测试您的自定义类BasicErrorController使用自定义ErrorAttributes实例配置的Spring BootRestTemplate对服务进行实际的HTTP调用| 归档时间: |
|
| 查看次数: |
3447 次 |
| 最近记录: |