Home > Mobile >  Problem with status 415 in Spring MockMVC Tests after trying to send a REST Post Request
Problem with status 415 in Spring MockMVC Tests after trying to send a REST Post Request

Time:12-21

I have a REST POST endpoint which is used to create an entity. I've trying to test it with MockMVC but every time that i sent the request i received a 415 status code (media not supported): java.lang.AssertionError: Status expected:<201> but was:<415> Expected :201 Actual :415

The endpoint accepts json body in the request and I sent this data using the MockMVC contentType as APPLICATION_JSON_VALUE and the content method with the serialized object by Jackson.

The controller ALSO is managed my Spring Security Filters but i think this is not the problem as i'm using the @AutoConfigureMockMvc(addFilters = false) and the HTTP status code is related to not supported media type and not about any security exception.

I've found a plenty of topics talking about it but none was able to solve my problem. One of the cases was including the @EnableWebMvc into the Controller OR as a configuration bean test, but none work it.

My attempt with @EnableWebMvc as test bean

@TestConfiguration
@EnableWebMvc
public class ProdutoControllerConfigurationTest {
    @Bean
    public ProdutoController produtoController() {
        return new ProdutoController(/* dependencies by autowired */);
    }
}

EDIT: I also tried with different MediaType like MediaType.APPLICATION_JSON and MediaType.APPLICATION_JSON_VALUE

My current tests:

@ActiveProfiles("testes")
@ExtendWith(SpringExtension.class)
@SpringBootTest
@WebAppConfiguration
@AutoConfigureMockMvc(addFilters = false)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
public class ProdutoControllerTest {

    @Autowired
    private MockMvc mockMvc;
    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void deveRetornarCreated_criacaoProdutoSucesso() throws Exception {
        CriarProdutoDTO criarProdutoDTO = new CriarProdutoDTO("Nome", new BigDecimal("2.0"), "DESCRIÇÃO", 2, 1);

        mockMvc.perform(MockMvcRequestBuilders.post("/api/produtos")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(objectMapper.writeValueAsString(criarProdutoDTO)))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isCreated());
    }

}

My Controller:

@RestController
@RequestMapping(value = "/api/produtos", produces=APPLICATION_JSON_VALUE, consumes=APPLICATION_JSON_VALUE)
public class ProdutoController {

    @Autowired
    private ProdutoService produtoService;
    @Autowired
    private CriarProdutoDtoToProdutoConverter produtoConverter;

    @PostMapping
    public void cadastrar(@RequestBody @Valid CriarProdutoDTO produtoDTO) {
        Produto novoProduto = produtoConverter.converter(produtoDTO);
        produtoService.cadastrar(novoProduto);
    }
}

CodePudding user response:

try add Accept header to your request

Accept=application/json

CodePudding user response:

I think you should use MediaType.APPLICATION_JSON instead of MediaType.APPLICATION_JSON_VALUE - which is the string representation of the enum.

  • Related