Spring Web Services Tutorial on Spring WS Unit Test Client

in this chapter, we will learn how to unit test a client created in the spring ws - writing client for the web application server created in chapter spring ws - writing server using spring ws.

step description
1 update the project countryservice under the package com.tutorialspoint as explained in the spring ws - writing server chapter.
2 create countryserviceclienttest.java under the package com.tutorialspoint under folder src → test → java as explained in steps given below.

countryserviceclienttest.java

package com.tutorialspoint;

import static org.junit.assert.*;

import org.junit.assert;
import org.junit.before;
import org.junit.test;
import org.springframework.oxm.jaxb.jaxb2marshaller;

import com.tutorialspoint.client.countryserviceclient;

public class countryserviceclienttest {
   countryserviceclient client;
   
   @before
   public void setup() throws exception {
      client = new countryserviceclient();
      jaxb2marshaller marshaller = new jaxb2marshaller();
      marshaller.setcontextpath("com.tutorialspoint");
      client.setmarshaller(marshaller);
      client.setunmarshaller(marshaller);
   }
   @test
   public void test() {
      getcountryresponse response = client.getcountrydetails("united states");
      country expectedcountry = new country();
      expectedcountry.setcapital("washington");
      country actualcountry = response.getcountry();
      assert.assertequals(expectedcountry.getcapital(), actualcountry.getcapital());
   }
}

start the web service

start the tomcat server and ensure we are able to access other webpages from the webapps folder using a standard browser.

unit test web service client

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 testing 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] changes detected - recompiling the module!
[warning] file encoding has not been set, using platform encoding cp1252, i.e. b
uild is platform dependent!
[info] compiling 10 source files to c:\mvn\countryservice\target\classes
[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] changes detected - recompiling the module!
[warning] file encoding has not been set, using platform encoding cp1252, i.e. b
uild is platform dependent!
[info] compiling 2 source files to c:\mvn\countryservice\target\test-classes
[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.countryserviceclienttest
feb 27, 2017 8:45:26 pm org.springframework.ws.soap.saaj.saajsoapmessagefactory
afterpropertiesset
info: creating saaj 1.3 messagefactory with soap 1.1 protocol
feb 27, 2017 8:45:26 pm org.springframework.oxm.jaxb.jaxb2marshaller createjaxbc
ontextfromcontextpath
info: creating jaxbcontext with context path [com.tutorialspoint]
tests run: 1, failures: 0, errors: 0, skipped: 0, time elapsed: 0.457 sec
running com.tutorialspoint.ws.customerendpointtest
feb 27, 2017 8:45:27 pm org.springframework.test.context.testcontextmanager retr
ievetestexecutionlisteners
info: @testexecutionlisteners is not present for class [class com.tutorialspoint
.ws.customerendpointtest]: using defaults.
feb 27, 2017 8:45:27 pm org.springframework.beans.factory.xml.xmlbeandefinitionr
eader loadbeandefinitions
info: loading xml bean definitions from class path resource [spring-context.xml]

feb 27, 2017 8:45:27 pm org.springframework.context.support.genericapplicationco
ntext preparerefresh
info: refreshing org.springframework.context.support.genericapplicationcontext@5
17c642: startup date [mon feb 27 20:45:27 ist 2017]; root of context hierarchy
feb 27, 2017 8:45:28 pm org.springframework.ws.soap.addressing.server.annotation
actionendpointmapping afterpropertiesset
info: supporting [ws-addressing august 2004, ws-addressing 1.0]
feb 27, 2017 8:45:28 pm 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.243 sec
feb 27, 2017 8:45:28 pm org.springframework.context.support.genericapplicationco
ntext doclose
info: closing org.springframework.context.support.genericapplicationcontext@517c
642: startup date [mon feb 27 20:45:27 ist 2017]; root of context hierarchy

results :

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

[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 5.686 s
[info] finished at: 2017-02-27t20:45:28+05:30
[info] final memory: 17m/173m
[info] ------------------------------------------------------------------------