java SOAP Web 服务单元/集成测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28836752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SOAP web service unit/integration testing
提问by Chiseled
My first version of the web service used plain JDBC
to connect to the underlying database. I had written my Unit tests for the application using JUnit
. I deployed this service on Jboss EAP 6.4
. So far so good.
我的第一个版本的 Web 服务使用普通JDBC
来连接到底层数据库。我已经使用JUnit
. 我在Jboss EAP 6.4
. 到现在为止还挺好。
I altered my application code to use Jboss's JDBC connection pool. It seems like Jboss 7+
does not allow referring to a data source externally (from outside the server). While the service still works fine , my unit tests are now broken. I am wondering how I can fix this.
我修改了我的应用程序代码以使用 Jboss 的 JDBC 连接池。似乎Jboss 7+
不允许从外部(从服务器外部)引用数据源。虽然该服务仍能正常工作,但我的单元测试现已损坏。我想知道如何解决这个问题。
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. I can then use JUnit to test the client. The problem is one has to manually create the stubs and every time the WSDL changes
我正在考虑重新编写相同的测试来测试服务而不是应用程序代码。一种方法是使用 wsimport 生成存根,然后编写客户端。然后我可以使用 JUnit 来测试客户端。问题是必须手动创建存根,并且每次 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
服务的最佳方式吗?
采纳答案by user2039709
You could use JaxWsProxyFactoryBeanto 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 (e.g. 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
- with an in-memory db (a little faster to execute)
- against a test-db similar to production
- 使用模拟数据库(几乎是 ws 的“纯”测试
- 使用内存数据库(执行速度稍快)
- 针对类似于生产的测试数据库
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();
}
}