简体   繁体   English

SOAP Web服务单元/集成测试

[英]SOAP web service unit/integration testing

My first version of the web service used plain JDBC to connect to the underlying database. 我的Web服务的第一个版本使用普通JDBC连接到基础数据库。 I had written my Unit tests for the application using JUnit . 我已经使用JUnit为应用程序编写了单元测试。 I deployed this service on Jboss EAP 6.4 . 我在Jboss EAP 6.4上部署了该服务。 So far so good. 到目前为止,一切都很好。

I altered my application code to use Jboss's JDBC connection pool. 我更改了应用程序代码以使用Jboss的JDBC连接池。 It seems like Jboss 7+ does not allow referring to a data source externally (from outside the server). 似乎Jboss 7+不允许从外部(从服务器外部)引用数据源。 While the service still works fine , my unit tests are now broken. 虽然该服务仍然可以正常运行,但是我的单元测试现在已损坏。 I am wondering how I can fix this. 我想知道如何解决此问题。

I was thinking of re-writing same tests to test the service instead of the application code. 我当时正在考虑重写相同的测试来测试服务而不是应用程序代码。 One way to do this would be to generate the stubs using wsimport and then write a client. 一种方法是使用wsimport生成存根,然后编写一个客户端。 I can then use JUnit to test the client. 然后,我可以使用JUnit来测试客户端。 The problem is one has to manually create the stubs and every time the WSDL changes 问题是必须手动创建存根,并且每次WSDL更改时

I am looking for an efficient way to accomplish this. 我正在寻找一种有效的方法来实现这一目标。 What would be ideal is a framework that accepts a url for the WSDL (or url to the service) and then allow me to call the service operations. 理想的情况是一个框架,该框架接受WSDL的URL(或服务的URL),然后允许我调用服务操作。

I am aware that the above is no longer unit tests but integration tests. 我知道以上不再是单元测试,而是集成测试。 Is this approach the best way to test a JAX-WS service ? 这是测试JAX-WS服务的最佳方法吗?

You could use JaxWsProxyFactoryBean to get a client automatically, its not fully dynamic, but more flexible than building the client manually. 您可以使用JaxWsProxyFactoryBean自动获取客户端,它不是完全动态的,但是比手动构建客户端更灵活。

Note : I use an abstract class for most of the setup & tests (eg constant test data, so I have 3 of these tests with varying setups 注意 :我对大多数设置和测试(例如恒定测试数据)使用抽象类,因此我有3种测试具有不同的设置

  • using a mocked db (nearly a "pure" test of the ws 使用模拟的db(几乎是对ws的“纯”测试)
  • with an in-memory db (a little faster to execute) 使用内存数据库(执行速度稍快)
  • against a test-db similar to production 针对类似于生产的测试数据库

which may help you as well, since it sounds you want some fine(r)-grained tests in some cases. 这也可能对您有帮助,因为在某些情况下,您似乎需要进行一些细粒度的测试。

Spring (test) config: 春季(测试)配置:

<?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"/>

Test Class 测试班

@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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM