Search code examples
javaspringspring-bootjunitspring-mvc-test

Unit tests - How to use Spring MockMVC to call soap service


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();
            });

Solution

  • 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)