Establishing a webservice unit test infrastructure using Unitils on Apache CXF and Hibernate
Unitils is most preferred Spring based unit testing framework for unit testing DAOs and persistence layers. However using the power of inherent Spring support it has, added with flexible flavors on JUnit or TestNG, it could be extended to implement Web-services testing infrastructure such as Apache CXF which has persistence layer access through DAO layer, that gets rid of loads of boilerplate code we got to ensure, before a proxy gets initialized.
This quick tip addresses how to put in place a unit testing infrastructure, so that unit testing Apache CXF web-services becomes a easy task. We assume we would use TestNG flavor of Unitils for our understanding, using JUnit is equivalently similar, and Maven based POM.
1) Include the Unitils dependency with TestNG in ${basedir}/pom.xml as given below.
<dependency> <groupId>org.unitils</groupId> <artifactId>unitils</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>5.8</version> <classifier>jdk15</classifier> <scope>test</scope> </dependency>
2) Add a Spring configuration file under ${basedir}/src/test/resources, say as client-test-config.xml and add the JAXWSProxyFactoryBean wiring so as to obtain a proxy object of the service exposed on a specific WSDL url.
<beans>
<bean id="propConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesMode" value="2"/>
<property name="location" value="classpath:mytest.properties"/>
</bean>
<bean id="wsclient" class="mypackage.MyServiceInterface" factory-bean="wsFactory" factory-method="create"/>
<bean id="wsFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
<property name="serviceClass" value="mypackage.MyServiceInterface"/>
<property name="address" value="${myservice.wsdl.url}"/>
</bean>
</beans>
3) Define a unit test-case using the Spring configuration file, to inject the proxy service bean, and invoke/unit test methods on the exposed service.
public class MyServiceTest extends UnitilsTestNG
{ @SpringApplicationContext
public ConfigurableApplicationContext createApplicationContext()
{ return new ClassPathXmlApplicationContext("client-test-config.xml");
}
@SpringBean("wsclient")
private MyServiceInterface myService;</span>
//All your unit test methods annotated with @Test here</span>
}
Thats it!. Your webservice unit test infrastructure for Apache CXF on Unitils is ready for adding test cases.