【问题标题】:Unit tests - How to use Spring MockMVC to call soap service单元测试——如何使用Spring MockMVC调用soap服务
【发布时间】:2018-06-26 21:55:46
【问题描述】:

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

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

我正在尝试使用类似的代码测试我的肥皂服务 (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();
            });

【问题讨论】:

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


    【解决方案1】:

    @WebMvcTest 注解只会加载应用的控制器层。这将仅扫描@Controller/@RestController 注释,不会加载完整的ApplicationContext

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

    【讨论】:

      【解决方案2】:

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

      post(URI).headers(headers)
      

      用适当的方法:

      get(URI).headers(headers)
      

      【讨论】:

        猜你喜欢
        • 2017-07-14
        • 2013-10-09
        • 2021-01-10
        • 2013-12-28
        • 2015-11-20
        • 2015-03-29
        • 1970-01-01
        • 2015-05-04
        • 1970-01-01
        相关资源
        最近更新 更多