私はSpring MVCテストを使用しています:私のテストケースでは、無効なBar
オブジェクト(age with zero)を渡しています。MethodArgumentNotValidException
がスローされていますが、 内にネストされていますNestedServletException
。私の現在のテストケースが合格するようにMethodArgumentNotValidException
、コントローラーから既存/カスタムを介して例外をスローする方法はありますか?HandlerExceptionResolver
checkHit2
コントローラ:
@RequestMapping(value="/test", method = RequestMethod.POST, headers="Accept=application/json")
@ResponseBody
public Bar getTables(@Valid @RequestBody Bar id) {
return id;
}
テストケース
@Before
public void setUp() {
mockMvc = standaloneSetup(excelFileUploader).setHandlerExceptionResolvers(new SimpleMappingExceptionResolver()).build();
}
@Test(expected=MethodArgumentNotValidException.class)
public void checkHit2() throws Exception {
Bar b = new Bar(0, "Sfd");
mockMvc.perform(
post("/excel/tablesDetail").contentType(
MediaType.APPLICATION_JSON).content(
TestUtil.convertObjectToJsonBytes(b)));
バー
public class Bar {
@JsonProperty("age")
@Min(value =1)
private int age;
public Bar(int age, String name) {
super();
this.age = age;
this.name = name;
}
...
}
ジャント出力
java.lang.Exception: Unexpected exception,
expected<org.springframework.web.bind.MethodArgumentNotValidException> but
was<org.springframework.web.util.NestedServletException>