java Spring MVC 测试框架因 HTTP 响应 406 失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14124110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:08:57  来源:igfitidea点击:

Spring MVC testframework fails with HTTP Response 406

javaspringtestingmodel-view-controller

提问by MatthiasLaug

I started to use the new MVC Testframework of Spring 3.2 and got stuck with getting 406 HTTP Response Codes for all my test cases.

我开始使用 Spring 3.2 的新 MVC 测试框架,并坚持为我的所有测试用例获取 406 个 HTTP 响应代码。

The testcase is plain simple

测试用例很简单

public class LocationResouceTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testGetLocationByPlzPattern() throws Exception {
        // here I need to define the media type as a static var from MediaType
        this.mockMvc.perform(get("/someurl?someparam=somevalue")).andExpect(status().isOk());
    }

}

the corresponding resource is

对应的资源是

@Controller
// here I need to define the media type as string
@RequestMapping(value = "/someurl", produces = "application/json; charset=UTF-8")
public class LocationResource {

    @ResponseBody
    @RequestMapping(method = RequestMethod.GET)
    public ArrayList<DTO> getAllIndex(@RequestParam("someparam") String param) {
        return ... //the list of DTO classes is transformed to json just fine if called with curl
    }

}

I am sure it is because of a wrong media type but I cannot figure out why.

我确定这是因为媒体类型错误,但我不知道为什么。

The trace of the failing testcase:

失败的测试用例的踪迹:

java.lang.AssertionError: Status expected:<200> but was:<406> at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60) at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89) at org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:546) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:141) at de.yourdelivery.rest.location.LocationResouceTest.testGetLocationByPlzPattern(LocationResouceTest.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

java.lang.AssertionError: Status expected:<200> but was: <406> at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60) at org.springframework.test.util.AssertionErrors.assertEquals( AssertionErrors.java:89) 在 org.springframework.test.web.servlet.result.StatusResultMatchers$5.match(StatusResultMatchers.java:546) 在 org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java: 141) at de.yourdelivery.rest.location.LocationResouceTest.testGetLocationByPlzPattern(LocationResouceTest.java:37) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorI) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorI) .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit。 runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.junit.internal.runners.statements.RunBefores.evaluate( RunBefores.java:28) 在 org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 在 org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:74) 83) 在 org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 在 org。springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 在 org.junit.runners.ParentRunner$3.run (ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner .access$000(ParentRunner.java:50) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 在 org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) ) 在 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 在 org.junit。runners.ParentRunner.run(ParentRunner.java:300) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run( JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java: 467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org .eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.run(TestExecution. java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.run(TestExecution. java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit。 runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit。 runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

回答by Rüdiger Schulz

If you have a @Configurationclass, you can add the @EnableWebMvcannotation instead of using an XML configuration with <mvc:annotation-driven />.

如果你有一个@Configuration类,你可以添加 @EnableWebMvc注释而不是使用带有<mvc:annotation-driven />.

回答by Arjan

This might be caused by not having any MVC configuration in the Spring test context. When using something like:

这可能是由于 Spring 测试上下文中没有任何 MVC 配置造成的。当使用类似的东西时:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("/test-context.xml")

...then that test-context.xmlfile should also include things like:

...那么该test-context.xml文件还应包括以下内容:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
  <mvc:message-converters>
    <bean id="..." class=
"org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter"/>
    <bean id="..."
      class="org.springframework.http.converter.StringHttpMessageConverter"/>
    <bean id="..."
      class="org.springframework.http.converter.FormHttpMessageConverter"/>
  </mvc:message-converters>
</mvc:annotation-driven>

<bean id="contentNegotiationManager" 
  class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
  <property name="defaultContentType" value="application/json" />
  <property name="mediaTypes">
    <value>
      json=application/json
      xml=application/xml
    </value>
  </property>
</bean>

One can easily test if the above is the problem, with a controller that does not specify anything special (such as: no producesin @RequestMapping), and hence does not need any content negotiation:

可以使用不指定任何特殊内容(例如: no producesin @RequestMapping)的控制器轻松测试上述问题是否存在,因此不需要任何内容​​协商:

@RequestMapping(value = "/foo", method = RequestMethod.GET)
@ResponseBody
public String getFoo() {
    return "bar";
}

...with:

...和:

@Test
public void getFoo() throws Exception {
    MvcResult result = 
      this.mockMvc.perform(get("/foo").accept(MediaType.TEXT_PLAIN))
        .andExpect(status().isOk())
        .andReturn();
    Assert.assertEquals(result.getResponse().getContentAsString(), "bar"); 
}

回答by Rajith Delantha

You need to add the following code to spring xml to serialize POJO in Hymanson.

需要在spring xml中添加以下代码来序列化Hymanson中的POJO。

<annotation-driven />

回答by Max Farsikov

It is necessary to have both @EnableWebMvcand .accept(MediaType.APPLICATION_JSON)

必须同时拥有@EnableWebMvc.accept(MediaType.APPLICATION_JSON)

回答by AlesMrak

You are missing the <mvc:annotation-driven />in the configuration xml file.

<mvc:annotation-driven />在配置 xml 文件中缺少。

回答by NimChimpsky

The spring way of doing things is, i think, to use a HttpResponseEntity, or return a modelandview. For example :

我认为,Spring 做事的方式是使用 HttpResponseEntity,或者返回一个模型和视图。例如 :

@ResponseBody
ResponseEntity<String> getFoo() {
  HttpHeaders responseHeaders = new HttpHeaders();
  responseHeaders.setContentType(MediaType.APPLICATION_JSON);
  String test = "{\"foo\":{\"title\": \"Stack\"}}";
  return new ResponseEntity<String>(test, responseHeaders, HttpStatus.OK);
}

(I'd be interested in any further progress you make as its all pretty new)

(我对你取得的任何进一步进展感兴趣,因为它都很新)

回答by Biju Kunjummen

I think the fix is to modify the request this way:

我认为解决方法是这样修改请求:

this.mockMvc.perform(get("/someurl?someparam=somevalue").contentType(MediaType.APPLICATION_JSON)).andExpect..

回答by Charles Shi

I met same problem just now, and is resolved by changing the return type of the Controller to String. And return a JSON string instead of an object directly. It does works.

我刚才也遇到了同样的问题,通过将Controller的返回类型更改为String来解决。并直接返回一个 JSON 字符串而不是一个对象。它确实有效。

Gson gson = new Gson();
return gson.toJson(dto);