title: 406 error on Spring MVC integration test, while the service works fine in browser
body: <p>I have a Spring 4 MVC web application. I am doing the Integration Test for my REST Services. The services are working fine from invoking through browser rest-clients and jquery/restangular UI as well. However, when I run my Integration Test, I am getting the error <code>Expected :200   Actual :406</code>. </p>
  
  <p>This issue comes only when Spring convert the Object to json. I tried manual conversion by ObjectMapper and returned the result. In this case, the test started working. 
  Here are my files:</p>
  
  <p>Controller:</p>
  
  <pre><code>@RequestMapping(value="/permissions", method = RequestMethod.GET)
      @ResponseBody
      public ResponseEntity&lt;Object&gt; getPermissions() throws Exception {
          return new ResponseEntity&lt;Object&gt;(getPermissions(), HttpStatus.OK);
      }
  </code></pre>
  
  <p>Integration Test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration
  @ContextConfiguration({"classpath*:rest-config.xml", "classpath:domain-config.xml"})
  public class ITController {
  
      @Autowired
      private WebApplicationContext context;
  
      private MockMvc mockMvc;
  
      @Before
      public void setup() {
          this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
      }
  @Test
      public void getPermissions() throws Exception {
          MvcResult result = this.mockMvc.perform(get("/permissions/4")
                  .accept("application/json"))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType("application/json")).andReturn();
          Permission p = new GsonBuilder().create().fromJson(result.getResponse().getContentAsString(), Permission.class);
          Assert.assertNotNull(p);
      }
  }
  </code></pre>
  
  <p>If I add the below code to the controller, then the test start working.</p>
  
  <pre><code>ObjectMapper mapper = new ObjectMapper();
  String stringVal = mapper.writeValueAsString(permission);
  </code></pre>
  
  <p>Part of my spring config file:</p>
  
  <pre><code>&lt;mvc:annotation-driven/&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
          &lt;property name="order" value="1"/&gt;
          &lt;property name="messageConverters"&gt;
              &lt;list&gt;
                  &lt;bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"&gt;
                      &lt;property name="supportedMediaTypes" value="application/json"/&gt;
                  &lt;/bean&gt;
                  &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter"&gt;
                      &lt;property name="supportedMediaTypes" value="text/plain;charset=UTF-8"/&gt;
                  &lt;/bean&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>I am using <code>jackson version 2.2.1</code> in my application.</p>
  
  <p><strong>DEBUG Log</strong></p>
  
  <pre><code>2014-07-14 14:41:37 DEBUG EntityPrinter:121 - com.myapp.entity.Permission{isActive=true, description=permission, id=4, Name=mypermission}
  2014-07-14 14:41:37 DEBUG JdbcTransaction:113 - committed JDBC Connection
  2014-07-14 14:41:37 DEBUG JdbcTransaction:126 - re-enabling autocommit
  2014-07-14 14:41:37 DEBUG LogicalConnectionImpl:314 - Releasing JDBC connection
  2014-07-14 14:41:37 DEBUG LogicalConnectionImpl:332 - Released JDBC connection
  2014-07-14 14:41:37 DEBUG ConnectionProxyHandler:219 - HHH000163: Logical connection releasing its physical connection
  </code></pre>