Testing CXF with Autowiring using Spring
Here is how to create a test for a CXF web service implementation class. The test use spring-mock base classes and hereby support stuff like transactions and autowiring through annotations. Neat.
I have a base class like this:
import org.springframework.test.jpa.AbstractJpaTests;
public abstract class AbstractCxfWsBeanTest extends AbstractJpaTests {
public static final String[] SERVICE_CONFIG_LOCATION = new String[]{
"classpath*:/applicationContext.xml",
"classpath:/datasourceContext.xml",
"classpath:/cxf.xml",
};
@Override
protected String[] getConfigLocations() {
return SERVICE_CONFIG_LOCATION;
}
}
One thing here: cxf.xml is loaded from classpath by the test code, but it resides in src/main/webapp/WEB-INF in my source tree, which is not exactly classpath stuff. I came around that with this little snippet in the <build> section of the maven pom.xml:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<includes>
<include>*</include>
</includes>
</testResource>
<testResource>
<directory>src/main/webapp/WEB-INF</directory>
<includes>
<include>cxf.xml</include> <!-- needed to make tests have cxf beans injected on them -->
</includes>
</testResource>
</testResources>
</build>
And finally, I can write a test like this one:
import org.springframework.beans.factory.annotation.Autowired;
public class TestMyWebService extends AbstractCxfWsBeanTest {
@Autowired
private MyWebService myWebService; // this is where I get my CXF WS impl injected
public void testFoo() {
assertEquals("bar", myWebService.foo());
}
}
June 20, 2010
Tags: cxf, Java, maven, spring, test Posted in: Programming

Leave a Reply