When I'm running JUnit the RestController does not accept JSON

0

I'm trying to do a test with MockMVC in my application, however, when I run the test it always gives this error:

org.springframework.web.HttpMediaTypeNotSupportedException

I noticed that when it is in scope of tests, the controller does not accept (Accept) json:

@RequestMapping(value = "/v1/teste", method = RequestMethod.POST)
public Saida teste(final @RequestBody(required = true) Object objeto) {}


@EnableWebMvc
@WebAppConfiguration
@ContextConfiguration("classpath:test-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class IntegracaoTest {

@Autowired
private WebApplicationContext wac;

private MockMvc mockMvc;

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

@Test
public void teste() throws Exception {
    this.mockMvc.perform(post("/v1/teste")
            .contentType(MediaType.APPLICATION_JSON).content("{}"))
            .andDo(print()).andExpect(status().is(200));
}

}

<tx:annotation-driven />

<context:annotation-config />
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.teste.*" />

<!-- Entity Manager -->
<bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:persistence.xml" />
    <property name="persistenceUnitName" value="teste" />
</bean>

<!-- Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<jpa:repositories base-package="com.teste.repositories"/>




MockHttpServletRequest:
     HTTP Method = POST
     Request URI = /v1/teste
      Parameters = {}
         Headers = {Content-Type=[application/json]}

         Handler:
            Type = com.teste.rest.TesteController$$EnhancerBySpringCGLIB$$c0dc19e2

           Async:
   Was async started = false
    Async result = null

  Resolved Exception:
            Type = org.springframework.web.HttpMediaTypeNotSupportedException

    ModelAndView:
       View name = null
            View = null
           Model = null

        FlashMap:

MockHttpServletResponse:
          Status = 415
   Error message = null
         Headers = {Accept=[application/octet-stream, */*, text/plain;charset=ISO-8859-1, */*, application/xml, text/xml, application/*+xml, application/x-www-form-urlencoded, multipart/form-data]}
    Content type = null
            Body = 
   Forwarded URL = null
  Redirected URL = null
         Cookies = []
    
asked by anonymous 17.08.2018 / 15:35

0 answers