2017-05-28 9 views
1

は、私は私のRESTコントローラで、次のポストルートを持っている:MockMvcテストPOSTリクエスト

@RequestMapping(value = "", method = RequestMethod.POST, produces = 
"application/json") 
public ResponseEntity saveMovie(@RequestBody Movie movie){ 
    movieService.saveMovie(movie); 
    return new ResponseEntity<Movie>(movie, HttpStatus.OK); 
} 

ルートがデータストアに要求本体に渡されたムービーを追加するサービスを使用しています。

Argument(s) are different! Wanted: 
com.stackroute.ng2boot.service.MovieService#0 bean.saveMovie(
    [email protected] 
); 
-> at 

com.stackroute.ng2boot.controllers.MovieRestControllerTest. 
saveMovie(MovieRestControllerTest.java:129) 
Actual invocation has different arguments: 
com.stackroute.ng2boot.service.MovieService#0 bean.saveMovie(
[email protected] 
); 
-> at  
com.stackroute.ng2boot.controllers.MovieRestController. 
saveMovie(MovieRestController.java:60) 

:私は次のエラーを取得する

@Test 
    public void saveMovie() throws Exception { 
    Movie movie1 = new Movie(); 
    movie1.setImdbID("imdb1"); 
    movie1.setTitle("Meter"); 
    movie1.setYear("2015"); 
    movie1.setPoster("meter.jpg"); 

    when(movieService.saveMovie(movie1)).thenReturn(movie1); 
    mockMvc.perform(post("/v1/api/movie") 
      .contentType(MediaType.APPLICATION_JSON_UTF8) 
      .content(asJsonString(movie1)) 
      .accept(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk())     
      .andExpect(content().contentType 
       (MediaType.APPLICATION_JSON_UTF8_VALUE)); 
      verify(movieService, times(1)).saveMovie(movie1); 
      verifyNoMoreInteractions(movieService); 
} 



public static String asJsonString(final Object obj) { 
    try { 
     final ObjectMapper mapper = new ObjectMapper(); 
     final String jsonContent = mapper.writeValueAsString(obj); 
     System.out.println(jsonContent); 
     return jsonContent; 
    } catch (Exception e) { 
     throw new RuntimeException(e); 
    } 
} 

:私は次のテストおよびそれのためのヘルパーメソッドを書かれている

Movie saveMovie(Movie movie); 

: サービスメソッドのシグネチャはこれです保存と更新を除いて、Movie JSONをリクエスト本体として渡す必要がある場合、他の経路はテストに合格します。貴重な情報をお伝えください。

ありがとうございます。

答えて

0

あなたが試すことができ:

import static org.mockito.Matchers.refEq; 
---- 
verify(movieService, times(1)).saveMovie(refEq(movie1));