title: Spring MVC returning 406 to controller test
body: <p>I have the following project tree:</p>
  
  <pre><code>├── app
  │   ├── build.gradle
  │   └── src
  │       ├── main
  │       │   ├── java
  │       │   │   └── child
  │       │   │       └── app
  │       │   │           └── Application.java
  │       │   └── resources
  │       │       └── application-default.yaml
  │       └── test
  │           └── java
  │               └── child
  │                   └── app
  │                       └── ApplicationTest.java
  ├── build.gradle
  └── childA
      ├── build.gradle
      └── src
          ├── main
          │   └── java
          │       └── child
          │           └── a
          │               ├── CoreConfig.java
          │               ├── PingController.java
          │               └── SpringWebMvcInitializer.java
          └── test
              └── java
              │   └── child
              │       └── a
              │           └── PingControllerTest.java
              └── resources
                  └── application-core.yml
  </code></pre>
  
  <p>I have the following test class : </p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @ContextConfiguration(classes = { PingController.class, SpringWebMvcInitializer.class }, initializers = ConfigFileApplicationContextInitializer.class)
  @WebAppConfiguration
  @ActiveProfiles({ "core" })
  public class PingControllerTest {
  
      private MockMvc mockMvc;
  
      @Autowired
      private WebApplicationContext webApplicationContext;
  
      @Before
      public void setUp() {
          mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
      }
  
      @Test
      public void testPing() throws Exception {
  this.mockMvc.perform(get(CoreHttpPathStore.PING)).andDo(print());
  this.mockMvc.perform(get(CoreHttpPathStore.PING).accept(MediaType.APPLICATION_JSON_UTF8_VALUE).contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)).andExpect(status().isOk());
      }
  
  }
  </code></pre>
  
  <p>I have the following <code>PingController.java</code>:</p>
  
  <pre><code>@RestController
  public class PingController {
  
      @RequestMapping(value = CoreHttpPathStore.PING, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
      public ResponseEntity&lt;Map&lt;String, Object&gt;&gt; ping() throws Exception {
          HashMap&lt;String, Object&gt; map = new HashMap&lt;&gt;();
          map.put("message", "Welcome to our API");
          map.put("date", new Date());
          map.put("status", HttpStatus.OK);
          return new ResponseEntity&lt;&gt;(map, HttpStatus.OK);
      }
  
  }
  </code></pre>
  
  <p>I have the following <code>SpringWebMvcInitializer.java</code>:</p>
  
  <pre><code>@ComponentScan
  public class SpringWebMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class&lt;?&gt;[]{};
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class&lt;?&gt;[]{};
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[]{ "/" };
      }
  
  }
  </code></pre>
  
  <h2>Expected</h2>
  
  <p>I expect test to pass with http code <code>200</code>.</p>
  
  <h2>Result</h2>
  
  <p>I get <code>406</code> and the following <code>print()</code>:</p>
  
  <pre><code>04:29:47.488 [main] DEBUG org.springframework.test.web.servlet.TestDispatcherServlet - Successfully completed request
  
  MockHttpServletRequest:
        HTTP Method = GET
        Request URI = /ping
         Parameters = {}
            Headers = {}
  
  Handler:
               Type = com.kopaxgroup.api.core.controller.PingController
  
  Async:
      Async started = false
       Async result = null
  
  Resolved Exception:
               Type = org.springframework.web.HttpMediaTypeNotAcceptableException
  
  ModelAndView:
          View name = null
               View = null
              Model = null
  
  FlashMap:
         Attributes = null
  
  MockHttpServletResponse:
             Status = 406
      Error message = null
            Headers = {}
       Content type = null
               Body = 
      Forwarded URL = null
     Redirected URL = null
            Cookies = []
  </code></pre>
  
  <h2>Question</h2>
  
  <p>So here are my questions : </p>
  
  <ul>
  <li><p>I have done some googling and most of the answers says that the context needs to be loaded with <code>@ContextConfiguration("/test-context.xml")</code>, I use JAVA configuration and I have no XML. Is my <code>SpringWebMvcInitializer.java</code> replacing that file and is it replacing it correctly ?</p></li>
  <li><p>Why do I get 406 error ?</p></li>
  </ul>