1

在尝试让我的 PUT MockMvc 测试工作时,我发现 JSON 不受支持。使用此线程的答案: 配置 MappingJacksonHttpMessageConverter

我能够通过扩展 WebMvcConfigurationSupport 类来解决这个问题。但是,当我使用此覆盖时,看起来好像在我的 GET 测试中返回的 ModelAndView 数据现在为 NULL。我知道我可以使用以下方法获取响应数据:

String content = result.getResponse().getContentAsString();

但是,有没有人解释为什么 ModelAndView 数据为 NULL?

这是我的 MockMVC 测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("test-rest-context.xml")
public class AccountControllerTest {


@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

private MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(),
        MediaType.APPLICATION_JSON.getSubtype(),
        Charset.forName("utf8"));

@Before
public void setUp() throws Exception {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}

@Test
public void findCustomerByID() throws Exception {
    MvcResult result = mockMvc.perform(get("/api/account/customers/{customerID}", "123").accept(contentType)
            .param("fields", "id", "email", "userName")
            .contentType(contentType))
            .andExpect(status().isOk())
            .andReturn();

    ModelAndView mav = result.getModelAndView();
    // mav is NULL here after I extend WebMvcConfigurationSupport class.

}    
}
4

0 回答 0