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

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

最佳答案

@WebMvcTest 注释将仅加载应用程序的 Controller 层。这将仅扫描 @Controller/@RestController 注释,并且不会加载完整的 ApplicationContext

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

关于java - 单元测试-如何使用Spring MockMVC调用soap服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48310929/

相关文章:

java - 存储层是否应该始终由服务层封装?

Spring Boot Camel Route - 从休息端点获取数据

java - Spring Boot创建数据源时出错

java - 部署 servlet 时出错( Jersey )

java - 声明一个具有多种类型的 ArrayList

java - 在 Spring MVC Controller 层中,@Scope ("prototype") 与 @Scope ("singleton")

spring - 如何在H2数据库中使用MySQL特有的函数?

java - @Query 标签中的嵌套 SQL 无法正常工作

java - 我如何使用 testng.xml 中的排除和包含来执行不同的类文件

spring - 如何在 spring 应用程序属性中获取正在运行的 tomcat 端口