2

I am unit testing my rest service using the code listed below and the request is successfully hitting the service.

     import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

     private MockMvc mvc;

     private URI = "/order/1"

     this.mvc.perform(
            post(URI).headers(headers)
                    .contentType(MediaType.APPLICATION_JSON_UTF8)
                    .content(mapper.writeValueAsString(request))
                    .accept(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(result -> {
                response[0] = result.getResponse().getContentAsString();
                statusCode[0] = result.getResponse().getStatus();
            });

I am trying to test my soap service (http://localhost:8080/OrderService/13.08.wsdl) using similar code. The endpoint seems to be up when I hit via browser , but seeing 404 in the result -

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

     private MockMvc mvc;

     private URI = "http://localhost:8080/OrderService/13.08.wsdl";

     this.mvc.perform(
            post(URI).headers(headers)
                    .contentType(MediaType.TEXT_XML)
                    .content(request.toString())
                    .accept(MediaType.TEXT_XML))
            .andExpect(result -> {
                response[0] = result.getResponse().getContentAsString();
                statusCode[0] = result.getResponse().getStatus();
            });
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315

2 Answers2

3

@WebMvcTest annotation will load only the controller layer of the application. This will scan only the @Controller/@RestController annotation and will not load the fully ApplicationContext.

Reference: https://springbootdev.com/2018/02/22/spring-boot-test-writing-unit-tests-for-the-controller-layers-with-webmvctest/

double-beep
  • 5,031
  • 17
  • 33
  • 41
Leo Milton
  • 31
  • 3
2

Your URI is a wsdl file to GET, and your code performs a POST against this URI. Try modifying your line of code :

post(URI).headers(headers)

with apropriate method :

get(URI).headers(headers)
aki
  • 91
  • 4