Java springboot:模拟soapweb服务
我想知道在SpringBoot中模拟SOAPWeb服务的最佳实践是什么,以便运行集成测试。我在spring网站上所能找到的就是。我们是否必须为像模拟依赖项这样简单的事情创建模式/wsdl 要模拟REST服务,我们所要做的就是将@RestController注释添加到我们的模拟服务中,以便引导它。我一直在寻找一个轻量级的解决方案 注意:我目前正在使用REST-Assured进行集成测试Java springboot:模拟soapweb服务,java,spring,spring-boot,soap,integration-testing,Java,Spring,Spring Boot,Soap,Integration Testing,我想知道在SpringBoot中模拟SOAPWeb服务的最佳实践是什么,以便运行集成测试。我在spring网站上所能找到的就是。我们是否必须为像模拟依赖项这样简单的事情创建模式/wsdl 要模拟REST服务,我们所要做的就是将@RestController注释添加到我们的模拟服务中,以便引导它。我一直在寻找一个轻量级的解决方案 注意:我目前正在使用REST-Assured进行集成测试 谢谢 最简单的方法是模拟负责与soapweb服务集成的bean 例如,如果您有一个使用Soap与另一个Web服务
谢谢 最简单的方法是模拟负责与soapweb服务集成的bean 例如,如果您有一个使用Soap与另一个Web服务进行通信的
SoapWebService
,那么您可以在测试中使用@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();
}
}
请尝试此模板:
import org.junit.*;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.test.context.ContextConfiguration;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入org.springframework.xml.transform.StringSource;
导入org.springframework.ws.test.client.MockWebServiceServer;
导入静态org.springframework.ws.test.client.RequestMatchers.*;
导入静态org.springframework.ws.test.client.ResponseCreators.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(“applicationContext.xml”)
公共类MyWebServiceClient集成测试{
//MyWebServiceClient扩展了WebServiceGatewaySupport,并在applicationContext.xml中配置
@自动连线
私有MyWebServiceClient客户端;
私有MockWebServiceServer mockServer;
@以前
public void createServer()引发异常{
mockServer=MockWebServiceServer.createServer(客户端);
}
@试验
public void getCustomerCount()引发异常{
Source expectedRequestPayload=新的StringSource(“某些预期的xml”);
Source responsePayload=newstringsource(“一些有效负载xml”);
expect(有效负载(expectedRequestPayload)).andRespond(带有效负载(responsePayload));
//getCustomerCount()使用WebServiceTemplate
int customerCount=client.getCustomerCount();
assertEquals(10,response.getCustomerCount());
mockServer.verify();
}
}
我使用WireMock模拟外部SOAP服务器依赖关系
测试类本身使用了@springbootest
注释来确保我拥有与真实环境相似的上下文
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
内容类型:应用程序/xop+xml;字符集=utf-8;type=“应用程序/soap+xml”
...
0
------=_零件_112_400566523.1602581633780
内容类型:应用程序/八位字节流
内容ID:
内容传输编码:二进制
...
------=_零件_112_400566523.1602581633780--
此设置使我能够尽可能多地模拟实际调用。我尝试了此设置,但得到了一个SaxException:尾部部分不允许包含内容。
------=_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--