Spring Web Services Tutorial on Spring WS Unit Test Server

in this chapter, we will understand how to unit test a web application service created by using the spring ws.

step description
1 update project countryservice created in the spring ws – write server chapter. add src/test/java folder.
2 create customerendpointtest.java under the – src/test/java/com/tutorialspoint/ws folder and then update the pom.xml as detailed below.
3 add spring-context.xml under the src/main/resources sub-folder.
4 the final step is to create content for all the source and configuration files and test the application as explained below.

pom.xml

<?xml version = "1.0" encoding = "utf-8"?>
<project xmlns = "http://maven.apache.org/pom/4.0.0" 
   xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance"
   xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 
   http://maven.apache.org/maven-v4_0_0.xsd">
   
   <modelversion>4.0.0</modelversion>
   <groupid>com.tutorialspoint</groupid>
   <artifactid>countryservice</artifactid>
   <packaging>war</packaging>
   <version>1.0-snapshot</version>
   <name>countryservice spring-ws application</name>
   <url>http://www.springframework.org/spring-ws</url>
   <build>
      <finalname>countryservice</finalname>
   </build>
   
   <dependencies>
      <dependency>
         <groupid>org.springframework.ws</groupid>
         <artifactid>spring-ws-core</artifactid>
         <version>2.4.0.release</version>
      </dependency>
      
      <dependency>
         <groupid>org.springframework</groupid>
         <artifactid>spring-test</artifactid>
         <version>2.5</version>
      </dependency>
      
      <dependency>
         <groupid>org.springframework.ws</groupid>
         <artifactid>spring-ws-test</artifactid>
         <version>2.4.0.release</version>
      </dependency>
      
      <dependency>
         <groupid>org.springframework</groupid>
         <artifactid>spring-tx</artifactid>
         <version>3.1.2.release</version>
      </dependency>
      
      <dependency>
         <groupid>jdom</groupid>
         <artifactid>jdom</artifactid>
         <version>1.0</version>
      </dependency>
      
      <dependency>
         <groupid>jaxen</groupid>
         <artifactid>jaxen</artifactid>
         <version>1.1</version>
      </dependency>
      
      <dependency>
         <groupid>wsdl4j</groupid>
         <artifactid>wsdl4j</artifactid>
         <version>1.6.2</version>
      </dependency>
      
      <dependency>
         <groupid>junit</groupid>
         <artifactid>junit</artifactid>
         <version>4.5</version>
         <scope>test</scope>
      </dependency>
   </dependencies>
</project>

spring-context.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:sws = "http://www.springframework.org/schema/web-services"
   xsi:schemalocation = "http://www.springframework.org/schema/beans
   
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/web-services
   http://www.springframework.org/schema/web-services/web-services-2.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package = "com.tutorialspoint"/>
   <sws:annotation-driven/>

   <bean id = "schema" class = "org.springframework.core.io.classpathresource">
      <constructor-arg index = "0" value = "countries.xsd" />
   </bean>
</beans>

customerendpointtest.java

package com.tutorialspoint.ws;

import javax.xml.transform.source;

import org.junit.before;
import org.junit.test;
import org.junit.runner.runwith;

import org.springframework.beans.factory.annotation.autowired;
import org.springframework.beans.factory.xml.xmlbeandefinitionreader;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.genericapplicationcontext;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;
import org.springframework.ws.test.server.mockwebserviceclient;
import org.springframework.xml.transform.stringsource;

import static org.springframework.ws.test.server.requestcreators.withpayload;
import static org.springframework.ws.test.server.responsematchers.payload;

@runwith(springjunit4classrunner.class)
@contextconfiguration( locations = "/spring-context.xml" )
public class customerendpointtest {
   @autowired
   private applicationcontext applicationcontext;
   private mockwebserviceclient mockclient;

   @before
   public void createclient() {
      mockclient = mockwebserviceclient.createclient(applicationcontext);
      genericapplicationcontext ctx = (genericapplicationcontext) applicationcontext;
      final xmlbeandefinitionreader definitionreader = new xmlbeandefinitionreader(ctx);
      definitionreader.setvalidationmode(xmlbeandefinitionreader.validation_none);
      definitionreader.setnamespaceaware(true);
   }
   @test
   public void testcountryendpoint() throws exception {
      source requestpayload = new stringsource(
         "<getcountryrequest xmlns = 'http://tutorialspoint/schemas'>"+
         "<name>united states</name>"+
         "</getcountryrequest>");				
      source responsepayload = new stringsource(
         "<getcountryresponse xmlns='http://tutorialspoint/schemas'>" +
         "<country>" +
         "<name>united states</name>"+
         "<population>46704314</population>"+
         "<capital>washington</capital>"+
         "<currency>usd</currency>"+
         "</country>"+
         "</getcountryresponse>");
      mockclient.sendrequest(withpayload(requestpayload)).andexpect(payload(responsepayload));
   }	
}

build the project

let us open the command console, go to the c:\mvn\countryservice directory and execute the following mvn command.

c:\mvn\countryservice>mvn test

maven will start building and test the project.

[info] scanning for projects...
[info]
[info] ------------------------------------------------------------------------
[info] building countryservice spring-ws application 1.0-snapshot
[info] ------------------------------------------------------------------------
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ countryser
vice ---
[warning] using platform encoding (cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[info] copying 2 resources
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ countryservice
---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-resources-plugin:2.6:testresources (default-testresources) @ co
untryservice ---
[warning] using platform encoding (cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[info] skip non existing resourcedirectory c:\mvn\countryservice\src\test\resour
ces
[info]
[info] --- maven-compiler-plugin:3.1:testcompile (default-testcompile) @ country
service ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-surefire-plugin:2.12.4:test (default-test) @ countryservice ---

[info] surefire report directory: c:\mvn\countryservice\target\surefire-reports

-------------------------------------------------------
 t e s t s
-------------------------------------------------------
running com.tutorialspoint.ws.customerendpointtest
feb 27, 2017 11:49:30 am org.springframework.test.context.testcontextmanager ret
rievetestexecutionlisteners
info: @testexecutionlisteners is not present for class [class com.tutorialspoint
.ws.customerendpointtest]: using defaults.
feb 27, 2017 11:49:30 am org.springframework.beans.factory.xml.xmlbeandefinition
reader loadbeandefinitions
info: loading xml bean definitions from class path resource [spring-context.xml]

feb 27, 2017 11:49:30 am org.springframework.context.support.genericapplicationc
ontext preparerefresh
info: refreshing org.springframework.context.support.genericapplicationcontext@b
2eddc0: startup date [mon feb 27 11:49:30 ist 2017]; root of context hierarchy
feb 27, 2017 11:49:31 am org.springframework.ws.soap.addressing.server.annotatio
nactionendpointmapping afterpropertiesset
info: supporting [ws-addressing august 2004, ws-addressing 1.0]
feb 27, 2017 11:49:31 am org.springframework.ws.soap.saaj.saajsoapmessagefactory
 afterpropertiesset
info: creating saaj 1.3 messagefactory with soap 1.1 protocol
tests run: 1, failures: 0, errors: 0, skipped: 0, time elapsed: 1.386 sec
feb 27, 2017 11:49:31 am org.springframework.context.support.genericapplicationc
ontext doclose
info: closing org.springframework.context.support.genericapplicationcontext@b2ed
dc0: startup date [mon feb 27 11:49:30 ist 2017]; root of context hierarchy

results :

tests run: 1, failures: 0, errors: 0, skipped: 0

[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 3.517 s
[info] finished at: 2017-02-27t11:49:31+05:30
[info] final memory: 11m/109m
[info] ------------------------------------------------------------------------