SOAP Web服务单元/集成测试

5
我的第一个Web服务版本使用普通的JDBC连接到底层数据库。我使用JUnit为应用程序编写了单元测试。我在Jboss EAP 6.4上部署了这个服务。到目前为止都很好。
我修改了应用程序代码,使用Jboss的JDBC连接池。似乎Jboss 7+不允许从服务器外部引用数据源。虽然服务仍然运行良好,但我的单元测试现在已经无效了。我想知道如何解决这个问题。
我考虑重新编写同样的测试来测试服务而不是应用程序代码。一种方法是使用wsimport生成存根,然后编写客户端。然后我可以使用JUnit测试客户端。问题是必须手动创建存根,并且每当WSDL更改时都要这样做。
我正在寻找一种有效的方法来完成此操作。理想情况下,框架接受WSDL的URL(或服务的URL),然后允许我调用服务操作。
我知道以上不再是单元测试,而是集成测试。这种方法是否是测试JAX-WS服务的最佳方式?

SOAP UI,你可以为Web服务调用设置测试用例,并运行WSI合规性报告。对于任何严肃的Web服务开发来说,这都是必备工具。 - Namphibian
1个回答

0

您可以使用JaxWsProxyFactoryBean自动获取客户端,它不是完全动态的,但比手动构建客户端更灵活。

注意:我在大多数设置和测试中使用抽象类(例如常量测试数据),因此我有3个这些测试,具有不同的设置

  • 使用模拟数据库(几乎是ws的“纯”测试
  • 使用内存数据库(执行速度稍快)
  • 针对类似于生产环境的测试数据库

这也可能对您有所帮助,因为听起来您想在某些情况下进行一些更细致的测试。

Spring(测试)配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:jaxws="http://cxf.apache.org/jaxws"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
   http://cxf.apache.org/jaxws
   http://cxf.apache.org/schemas/jaxws.xsd">

<jaxws:endpoint id="myService"
                implementor="#serviceBean"
                address="http://localhost:9000/MyService"/>

<!-- id is used so we can access this via @Inject @Qualifier("serviceClientId") in the test class -->
<bean id="serviceClientId" class="package.MyService"
      factory-bean="proxyFactory"
      factory-method="create"/>

<bean id="proxyFactory"
      class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
    <property name="serviceClass" value="package.MyService"/>
    <property name="address" value="http://localhost:9000/DeliveryService"/>
</bean>

<bean id="deliveryServiceBean" class="package.MyServiceImpl"/>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-config.xml"})
@TestExecutionListeners(listeners = {TransactionalTestExecutionListener.class, ServletTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,})
@Transactional
public class TestIntegrationMyService extends TestMyService {

    @Inject
    @Qualifier("serviceClientId")
    public void setClient(MyService client) {
        this.client = client;
    }

    @Test
    public void validRequestShouldWork() throws Exception {
        client.doSomething();
    }
}

谢谢!我会尝试这个。 - Chiseled
我使用SOAPUI API编写了我的集成测试,并将其创建为Maven项目。获取所有依赖项可能有些麻烦,但一旦您完成了这一步骤,就可以顺利进行下一步。http://www.soapui.org/developers-corner/integrating-with-soapui.html - Chiseled
所以,这样就更加灵活了,因为您不需要(Java) WS接口(如果有的话),但我猜使用这种设置构建更复杂的请求体可能会有些繁琐? - user2039709

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接