1

これは私のコントローラーメソッドです:

    @RequestMapping(method = RequestMethod.PUT,produces="application/json", headers = "content-type=application/json")
    public @ResponseBody User updateUser(@RequestBody User user) throws PropertyErrorException, ItemNotFoundException {         
        return service.UpdateUser(user);     
    }

Spring test-mvc を使用して、単体テスト ケースを書きたいと思います。

@Test
public void UpdateUser() throws Exception {     
    mockMvc.perform(put("/r01/users").body(userJsonString.getBytes())
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().type(MediaType.APPLICATION_JSON))
        .andExpect(content().string(userString));
}

このテスト ケースを実行すると、以下が生成されます。

WARN org.springframework.web.servlet.PageNotFound - Request method 'PUT' not supported

また、updateUser メソッドは呼び出されず、応答コードは 405 です。

私は多くのテスト、すべての GET リクエストを作成しましたが、それらは正しく機能しています。これは、ContextConfiguration に自信があることを意味します。私は何を逃したのですか?

4

1 に答える 1

3

@RequestMapping メソッドでコンテンツタイプのヘッダーを明示的に期待していますが、リクエストでコンテンツタイプを送信していません。それがリクエストが失敗する理由だと思います..これを試してください。

mockMvc.perform(put("/r01/users").contentType(MediaType.APPLICATION_JSON).body(userJsonString.getBytes())
    .accept(MediaType.APPLICATION_JSON))
    .andExpect(status().isOk())
    .andExpect(content().type(MediaType.APPLICATION_JSON))
    .andExpect(content().string(userString));
于 2012-09-06T19:18:35.133 に答える