刚刚使用CXF实现了SOAP Web服务。我很容易使用模拟框架编写一些单元测试。但不太确定为我的Web服务编写一些集成测试的最佳方法是什么。实现是这样的:
@Autowired
private InvoiceService invoiceService;
@Webservice(endpointinterface="xxx")
public Invoice retrieveInvoiceById(String id) {
Invoice invoice = invoiceService.getInvoiceById(id);
return invoice;
}
InvoiceService将调用该方法从文本文件或某个文件系统中检索发票,然后返回。那么我应该如何编写集成测试来测试整体呢?
谢谢你们。
答案 0 :(得分:1)
编写单元测试,以便实际测试将启动Jetty服务器并在测试运行期间将Web服务公开为真实端点。如果您使用的是任何数据库,请使用Derby或其他支持内存中功能的数据库。
例如只需在测试上下文spring文件中声明您的端点:
<jaxws:endpoint id="someProxy"
implementor="#yourWebServiceImplBean"
wsdlLocation="src/main/webapp/WEB-INF/wsdl/InvoiceService.wsdl"
address="http://0.0.0.0:12345/YourService/services/InvoiceService"/>
这足以启动Jetty实例并公开您的Web服务。这将在端口12345
上启动Jetty实例。将此bean自动装配到您的测试类中,您就可以调用方法了。
此外,您需要包含此依赖项,以便Jetty在单元测试中运行。
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>${cxf.version}</version>
<scope>test</scope>
</dependency>