Spring MockMvc似乎不支持"生产"在使用WebApplicationInitializer

时间:2015-11-30 13:20:16

标签: spring spring-test-mvc

我有以下方法:

@RequestMapping(value = "/report.txt", produces = "text/plain;charset=UTF-8", method = RequestMethod.GET)
@ResponseBody
public String showReport(
  ...
)

我测试的是:

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

@Test
public void reportPage() throws Exception {
    mockMvc.perform(get("/report.txt"))
        .andDo(print())
        .andExpect(status().isOk())
        .andExpect(content().contentType("text/plain;charset=UTF-8"))

.andDo(print())告诉我     Content-Type=[text/plain;charset=ISO-8859-1]

似乎"中的内容类型产生" MockMvc不使用该属性。当我在jetty中运行应用程序时,此页面的内容类型是正确的。

这是在我们开始使用WebApplicationInitializer之后开始的。

我在春季4.2.2.RELEASE。

有没有其他人在这看到过这个问题?任何解决方法的想法?

谢谢!

-Kaj:)

1 个答案:

答案 0 :(得分:0)

        this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
        .andExpect(content().contentType(MediaType.valueOf("text/plain;charset=ISO-8859-1")))
        .andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
        .andExpect(content().contentTypeCompatibleWith("text/plain"))
        .andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN));

从此链接https://github.com/spring-projects/spring-framework/blob/master/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/ContentAssertionTests.java