2016-03-31 15 views
1

私は@RestControllerを1つ作成するので、検証をテストする必要があります。そのため、1つのモデルを作成して値を設定しないでください。そうすれば、多くのエラーが発生するので、この検証をテストします。スプリングブート統合テストRestController検証

私が検証するために、このメソッドを作成します。

@Test 
    public void selarAberturaMatriculaFail() throws Exception { 
     AberturaMatricula aberturaMatricula = new AberturaMatricula(); 
     MockHttpServletRequestBuilder requ = post("/ri/selo/abertura/").contentType(contentType).content(this.jsonWithClass(aberturaMatricula)); 
     mockMvc.perform(requ) 
       .andExpect(model().attributeHasErrors("cns")); 
    } 

をしかし、私はこのエラーを得た:

No ModelAndView found java.lang.AssertionError: No ModelAndView found

これは私の残りの方法であって、私のモデルでは

@RestController 
@RequestMapping("/ri") 
public class RIController { 
@RequestMapping(value = "/selo/abertura/", method = RequestMethod.POST) 
    public AberturaMatricula selarAbertura(@RequestBody @Valid AberturaMatricula aberturaMatricula){ 

... 
    } 
} 

私はCNSを持っていますプロパティなど。

答えて

0

RESTはモデルとビューではなくオブジェクトを返すだけなので、RESTコントローラーでモデルテストを使用することは望ましくありません。例としてhttps://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#spring-mvc-test-frameworkを参照してください。このチュートリアルhttp://spring.io/guides/tutorials/bookmarks/は、RESTサービスのためのいくつかのテストを構築する方法も示しています。