java - SOAP Web 服务单元/集成测试

标签 java web-services unit-testing jax-ws integration-testing

我的第一个 Web 服务版本使用纯 JDBC 连接到底层数据库。我已经使用 JUnit 为应用程序编写了单元测试。我在 Jboss EAP 6.4 上部署了这个服务。到目前为止一切顺利。

我更改了我的应用程序代码以使用 Jboss 的 JDBC 连接池。似乎 Jboss 7+ 不允许从外部(从服务器外部)引用数据源。虽然该服务仍然可以正常工作,但我的单元测试现在已损坏。我想知道如何解决这个问题。

我正在考虑重新编写相同的测试来测试服务而不是应用程序代码。一种方法是使用 wsimport 生成 stub ,然后编写客户端。然后我可以使用 JUnit 来测试客户端。问题是必须手动创建 stub 并且每次 WSDL 更改时

我正在寻找一种有效的方法来完成此任务。理想的框架是接受 WSDL 的 url(或服务的 url),然后允许我调用服务操作。

我知道以上不再是单元测试而是集成测试。这种方法是测试 JAX-WS 服务的最佳方式吗?

最佳答案

你可以使用 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();
    }
}

关于java - SOAP Web 服务单元/集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28836752/

相关文章:

SharePoint 2010 Web 服务上的 Java JBoss 401 错误

Python Mock.Patch 在多个地方使用的函数

c# - AuthorizeAttribute 的 IsAuthorized 在单元测试中始终为 false

java - 获得数据库更新和更新提要的通知

java - Android - 上传图像和请求数据

java - 来自 WSDL 的 Web 服务

javascript - Nant + Javascript 单元测试

java - 具有组合键的 JPA OneToMany 和 ManyToOne 正在生成第三个表

java - 查询java.util.Hashtable的实现细节

android - String.equals() 总是返回 false?