Archive

Posts Tagged ‘spring-test’

Unit testing CXF webservice endpoints using Spring-Test and TestNG

October 9th, 2009 Shrihari No comments

The Spring IOC feature of configurational wiring of POJOs to control and achieve network of business logic frees developers of writing any boilerplate code and eventually of bugs. I would like to explain one such facility with regard to establishing an unit testing infrastructure for writing unit test cases for JAXWS specific web service endpoints (CXF) using Spring-test module and TestNG unit testing framework.

Lets take a simple example of a web service endpoint exposing a functionality to query a book price, given the ISBN number, given a maven project. This write-up just concentrates on establishing a web service unit testing framework. Following are sequence of steps one could follow to achieve the required infrastructure setup.

1) Change ${basedir}/pom.xml include adding spring-test dependency, and TestNG dependency, with maven-surefire-plugin configuration (assuming CXF dependencies are already present)

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>2.5.6</version>
 </dependency>
<dependency>
 <groupId>org.testng</groupId>
 <artifactId>testng</artifactId>
 <version>5.9</version>
 <classfier>jdk15</classifier>
 <scope>test</scope>
</dependency>

2) Add a Spring configuration file (service-test.xml) under ${basedir}/src/test/resources. The CXF’s JaxWSProxyFactoryBean can be wired to to create a service proxy. Below is the snippet of the configuration file.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                    http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"></span></pre>
    <import resource="classpath:META-INF/cxf/cxf.xml" />
     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
     <import resource="classpath:META-INF/cxf/cxf-extension-http.xml" />

     <!-- Required to load the service endpoint uris -->
     <bean id="propConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations">
            <list>
               <value>classpath:service-test.properties</value>
             </list>
         </property>
     </bean>

     <bean id="proxyFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
         <property name="serviceClass" value="com.schtech.service.endpoint.store.BookPriceService"/>
         <property name="address" value="${bookstore.service.endpoint.url}"/>
         <property name="bus" ref="cxf" />
    </bean>

     <bean id="serviceClient" class="com.schtech.service.endpoint.store.BookPriceService" factory-bean="proxyFactory"
        factory-method="create"/>
</beans>

3) Implement the TestNG test case to test web service. Here three things have to be noted: (1) ContextConfiguration annotation helps us load
the configuration file. (2) The test case extends AbstractTestNGSpringContextTests to load the context configuration and autowire the service
bean. (3) Autowire annotation on the service endpoint interface initializes with proxy factory created service proxy instance.

@ContextConfiguration(locations = {"classpath:service-context.xml"})
public class BookCatalogTest extends AbstractTestNGSpringContextTests
 { @Autowired
   private BookPriceService serviceClient;

   @Test
   public void testBookPrice()
   { String isbn = "978-3-16-148410-0,";
     Double price  = serviceClient.findBookPrice(isbn);
     assertNotNull(price);
   }
}

The infrastructure is ready to be used to test other service endpoints hosted elsewhere. Running mvn test at the ${basedir} prompt would unit test the service endpoints.

Categories: jee-light Tags: , ,