[英]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. 我正在使用下面列出的代码对我的rest服务进行单元测试,并且请求成功达到该服务。
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. 我正在尝试使用类似的代码测试我的肥皂服务( http:// localhost:8080 / OrderService / 13.08.wsdl )。 The endpoint seems to be up when I hit via browser , but seeing 404 in the result - 当我通过浏览器命中时,端点似乎已启动,但在结果中看到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();
});
Your URI is a wsdl file to GET, and your code performs a POST against this URI. 您的URI是GET的wsdl文件,并且您的代码针对此URI执行POST。 Try modifying your line of code : 尝试修改您的代码行:
post(URI).headers(headers)
with apropriate method : 用适当的方法:
get(URI).headers(headers)
@WebMvcTest
annotation will load only the controller layer of the application. @WebMvcTest
批注将仅加载应用程序的控制器层。 This will scan only the @Controller
/ @RestController
annotation and will not load the fully ApplicationContext
. 这将仅扫描@Controller
/ @RestController
批注,并且不会加载完整的ApplicationContext
。
Reference: https://springbootdev.com/2018/02/22/spring-boot-test-writing-unit-tests-for-the-controller-layers-with-webmvctest/ 参考: https : //springbootdev.com/2018/02/22/spring-boot-test-writing-unit-tests-for-the-controller-layers-with-webmvctest/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.