Archive

Posts Tagged ‘unitils’

Establishing a webservice unit test infrastructure using Unitils on Apache CXF and Hibernate

July 16th, 2009 Shrihari No comments

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.

Categories: jee-light Tags: ,

Implementing a Unitils DAO testing infrastructure for Spring and JPA on TestNG

June 29th, 2009 Shrihari No comments

Unitils provides an excellant infrastructure for unit testing DAO (and data-service) layers  for Spring as IoC Framework on all the component unit testing frameworks such as JUnit 3, JUnit4 and TestNG, without any boilerplate code or coniguration hassles. We will look at establishing a testing infrastructure for Spring with JPA on TestNG frmaework.

Suppose the custom DAO and its implementation definition are like this (with regard to semantics of Spring JPA integration)

package mypackage;
public CustomDAO
{    public List findAll();
     public void save(Entity eo);
}
package mypackage;
public CustomDAOImpl implements CustommmDAO
{   @PersistenceContext
    private EntityManager em;
    //all other method implementations..
      ....
}

In order to setup a Unitils test infrastructure on TestNG, the following is the class-template:

public class CustomDAOTestCase extends UnitilsTestNG
{   /**
     * Injects a test specific application context configuration
     */
    @SpringApplicationContext
    public ConfigurableApplicationContext createApplicationContext()
    {  return new ClassPathXmlApplicationContext("applicationContext-test.xml");
    }
     @SpringBean("customDAO")
     private CustomDAO customDAO;
     //all @Test annotated methods..
      @Override
      protected void unitilsAfterTestTearDown(java.lang.reflect.Method method)
      {}
}

for which the following would be the test specific Spring’s application context confguration (applicationContext-test.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

 <bean id="entityManagerFactory" >
 <property name="persistenceXmlLocation" value="persistence-test.xml"/>
 <property name="persistenceUnitName" value="customservice-test"/>
 </bean>

 <bean id="transactionManager">
 <property name="entityManagerFactory" ref="entityManagerFactory" />
 </bean>

 <tx:annotation-driven />

 <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

 <bean id="customDAO" class="mypackage.CustomDAOImpl"/>
</beans>

Look specifically at entityManagerFactory bean definition, org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean allows us to load test specific persistence.xml which could allow us to maintain more than onne persistence context for a sample application.
These two definitions extend the Spring’s JPA capabilities for unit testing DAO on Unitils infrastructure!

Categories: Uncategorized Tags: , ,