[英]Spring Boot: Mocking SOAP Web Service
我想知道在 Spring Boot 中模擬 SOAP Web 服務的最佳實踐是為了運行集成測試。 我在 spring 網站上能找到的只有https://spring.io/guides/gs/sumption-web-service/ 。 我們是否必須為像模擬依賴這樣簡單的事情創建模式/wsdl?
要模擬 REST 服務,我們所要做的就是將 @RestController 注釋添加到我們的模擬服務中以使其啟動。 我正在尋找一種輕量級的解決方案。
注意:我目前使用 REST Assured 進行集成測試。
謝謝!
我使用 WireMock 來模擬外部 SOAP 服務器依賴項。
測試類本身使用@SpringBootTest
注釋來確保我有與真實環境相似的上下文。
private WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8089));
@Autowired
SoapClient soapClient;
@Test
@DisplayName("Retrieve SOAP message")
void retrieveMessage() {
wireMockServer.start();
WireMock.configureFor("localhost", 8089);
WireMock.reset();
stubFor(post(urlEqualTo("/ECPEndpointService"))
.willReturn(
aResponse()
.withStatus(200)
.withHeader("Content-Type",
"Multipart/Related; boundary=\"----=_Part_112_400566523.1602581633780\"; type=\"application/xop+xml\"; start-info=\"application/soap+xml\"")
.withBodyFile("RawMessage.xml")
)
);
soapClient.retrieveActivations();
wireMockServer.stop();
}
RawMessage.xml
的內容是消息響應。 在我的情況下,它是一個多部分消息(簡化):
------=_Part_112_400566523.1602581633780
Content-Type: application/xop+xml; charset=utf-8; type="application/soap+xml"
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns5:ReceiveMessageResponse xmlns:ns3="http://mades.entsoe.eu/" xmlns:ns4="http://mades.entsoe.eu/2/" xmlns:ns5="http://ecp.entso-e.eu/" xmlns:ns6="http://ecp.entsoe.eu/endpoint">
<receivedMessage>
...
</receivedMessage>
<remainingMessagesCount>0</remainingMessagesCount>
</ns5:ReceiveMessageResponse>
</env:Body>
</env:Envelope>
------=_Part_112_400566523.1602581633780
Content-Type: application/octet-stream
Content-ID: <7a2f354f-dc52-406b-a4b1-9d89aa29cb2d@null>
Content-Transfer-Encoding: binary
<?xml version="1.0" encoding="UTF-8"?>
<edx:Message
xmlns:edx="http://edx.entsoe.eu/internal/messaging/message"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<edxMetadata>
...
</edxMetadata>
<content v="PEFjdGl2YXRpb25"/>
</edx:Message>
------=_Part_112_400566523.1602581633780--
這種設置使我能夠盡可能地模擬真實通話。
試試這個模板:
import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.xml.transform.StringSource;
import org.springframework.ws.test.client.MockWebServiceServer;
import static org.springframework.ws.test.client.RequestMatchers.*;
import static org.springframework.ws.test.client.ResponseCreators.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("applicationContext.xml")
public class MyWebServiceClientIntegrationTest {
// MyWebServiceClient extends WebServiceGatewaySupport, and is configured in applicationContext.xml
@Autowired
private MyWebServiceClient client;
private MockWebServiceServer mockServer;
@Before
public void createServer() throws Exception {
mockServer = MockWebServiceServer.createServer(client);
}
@Test
public void getCustomerCount() throws Exception {
Source expectedRequestPayload = new StringSource("some expected xml");
Source responsePayload = new StringSource("some payload xml");
mockServer.expect(payload(expectedRequestPayload)).andRespond(withPayload(responsePayload));
// client.getCustomerCount() uses the WebServiceTemplate
int customerCount = client.getCustomerCount();
assertEquals(10, response.getCustomerCount());
mockServer.verify();
}
}
最簡單的方法是模擬負責與 Soap Web Service 進行集成的 bean。
例如,如果您有一個SoapWebService
使用 Soap 與另一個 Web 服務進行此通信,則可以在測試中使用@MockBean
注釋並模擬返回。 示例:
@SpringBootTest
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class YourControllerIT {
@MockBean
private SoapWebService soapWebService ;
@Before
public void setup() {
when(soapWebService.soapCall(
any(), anyLong())).thenReturn("mockedInformation");
}
@Test
public void addPerson() {
MvcResult mvcResult = mockMvc.perform(post("/api/persons")
.accept("application/json")
.header("Content-Type", "application/json")
.content(jsonContent))
.andExpect(status().isCreated())
.andReturn();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.