单元测试 - 如何使用Spring MockMVC来调用soap服务

时间:2018-01-17 22:17:35

标签: java spring spring-boot junit spring-mvc-test

我使用下面列出的代码对我的休息服务进行单元测试,并且请求已成功访问该服务。

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

我正在尝试使用类似代码测试我的soap服务(http://localhost:8080/OrderService/13.08.wsdl)。当我通过浏览器点击时,端点似乎已经启动,但在结果中看到404 -

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

2 个答案:

答案 0 :(得分:2)

您的URI是GET的wsdl文件,您的代码会针对此URI执行POST。 尝试修改您的代码行:

post(URI).headers(headers)

采用适当的方法:

get(URI).headers(headers)

答案 1 :(得分:2)

@WebMvcTest批注将仅加载应用程序的控制器层。这将仅扫描@Controller / @RestController批注,而不会加载完整的ApplicationContext

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

相关问题