[英]Unit tests - How to use Spring MockMVC to call soap 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();
});
我正在嘗試使用類似的代碼測試我的肥皂服務( 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();
});
您的URI是GET的wsdl文件,並且您的代碼針對此URI執行POST。 嘗試修改您的代碼行:
post(URI).headers(headers)
用適當的方法:
get(URI).headers(headers)
@WebMvcTest
批注將僅加載應用程序的控制器層。 這將僅掃描@Controller
/ @RestController
批注,並且不會加載完整的ApplicationContext
。
參考: 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.