我正在为我的@RequestMapping方法的自定义错误场景设计一个 POJO 。
Spring 默认抛出 a HttpMessageNotReadableExceptionif a POSTis made without any body 并很好地返回错误响应,如下所示:
{
"timestamp": "2017-07-28T18:54:11.867+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Required request body is missing: public org.springframework.http.ResponseEntity<some.package.name.SomeResponseClass> some.package.name.SomeControllerClass.someRequestMappingMethod(java.util.Map<java.lang.String, java.lang.String>, some.package.name.SomeRequestClass)",
"path": "/somepath/"
}
Run Code Online (Sandbox Code Playgroud)
类似地,如果对任何注释的字段的验证@Valid失败,它会抛出MethodArgumentNotValidException并返回错误响应,如下所示:
{
"timestamp": "2017-07-28T17:23:53.102+0000",
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MethodArgumentNotValidException",
"errors": [
{
"codes": [
"NotNull.incident.someField",
"NotNull.someField",
"NotNull.java.lang.String",
"NotNull"
],
"arguments": [
{
"codes": [
"someObject.someField",
"someField"
],
"arguments": null,
"defaultMessage": "someField",
"code": …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试应该使用自定义错误属性的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)
关于我如何测试自定义属性的任何线索?
鉴于以下有效负载:
data public class CandidateDetailDTO(val id: String,
val stageName: String,
val artists: Iterable<ArtistDTO>,
val instruments: Iterable<InstrumentDTO>,
val genres: Iterable<GenreDTO>,
val discoverable: Boolean,
val gender: Gender,
val involvement: Involvement,
val biography: String,
var photoURLs: List<URL>,
var birthday: Date? = null,
var customGenre: String? = null)
Run Code Online (Sandbox Code Playgroud)
..如图所示,某些字段允许为空,其他字段不允许.
使用Spring Boot调用请求时,如果缺少预期字段,则返回400 - Bad Request.这是不太令人期待的,我期望相关的控制器建议适用:
@ControllerAdvice
public class SomeExceptionHandler : ResponseEntityExceptionHandler()
{
@ExceptionHandler(Throwable::class)
@ResponseBody
public fun onException(ex: Throwable): ResponseEntity<ErrorResponse>
{
val responseCode = ex.responseCode()
val errorResponse = ErrorResponse(response = ResponseHeader(responseCode, ex.message))
return ResponseEntity(errorResponse, responseCode.httpStatus());
} …Run Code Online (Sandbox Code Playgroud)