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 Create a project with a name leaveService under a package com.tutorialspoint as explained in the Spring WS - First Application chapter.
2 Create a Java Classes CustomerEndPointTest.java under the com.tutorialspoint.ws package under src/test/java folder.
3 Create countries.xsd and spring-context.xml under the src/main/resources folder.
4 The final step is to create content of all source and configuration files and export 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/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.tutorialspoint.hr</groupId>
   <artifactId>leaveservice</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <name>leaveService Spring-WS Application</name>
   <url>http://www.springframework.org/spring-ws</url>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>21</maven.compiler.source>
      <maven.compiler.target>21</maven.compiler.target>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.ws</groupId>
         <artifactId>spring-ws-core</artifactId>
         <version>4.0.11</version>
      </dependency>
      <dependency>
         <groupId>org.jdom</groupId>
         <artifactId>jdom2</artifactId>
         <version>2.0.6.1</version>
      </dependency>
      <dependency>
         <groupId>jaxen</groupId>
         <artifactId>jaxen</artifactId>
         <version>2.0.0</version>
      </dependency>
      <dependency>
         <groupId>wsdl4j</groupId>
         <artifactId>wsdl4j</artifactId>
         <version>1.6.3</version>
      </dependency>
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter</artifactId>
         <version>5.10.0</version>
      </dependency>
      <dependency>
         <groupId>org.springframework.ws</groupId>
         <artifactId>spring-ws-test</artifactId>
         <version>4.0.11</version>
      </dependency>
   <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>6.0.0</version>
   </dependency>
   </dependencies>
   <build>
      <finalName>leaveservice</finalName>
         <pluginManagement>
            <plugins>
               <plugin>
                  <artifactId>maven-clean-plugin</artifactId>
                  <version>3.4.0</version>
            </plugin>
            <plugin>
               <artifactId>maven-resources-plugin</artifactId>
               <version>3.3.1</version>
            </plugin>
            <plugin>
               <artifactId>maven-compiler-plugin</artifactId>
               <version>3.13.0</version>
            </plugin>
            <plugin>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.3.0</version>
            </plugin>
            <plugin>
               <artifactId>maven-war-plugin</artifactId>
               <version>3.4.0</version>
            </plugin>
            <plugin>
               <artifactId>maven-install-plugin</artifactId>
               <version>3.1.2</version>
            </plugin>
            <plugin>
               <artifactId>maven-deploy-plugin</artifactId>
               <version>3.1.2</version>
            </plugin>
         </plugins>
      </pluginManagement>
   </build>
</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 static org.springframework.ws.test.server.RequestCreators.withPayload;
import static org.springframework.ws.test.server.ResponseMatchers.payload;

import javax.xml.transform.Source;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.junit.jupiter.SpringExtension;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.xml.transform.StringSource;

@ExtendWith(SpringExtension.class)
@ContextConfiguration( locations = "/spring-context.xml" )
public class CustomerEndPointTest {
   @Autowired
   private ApplicationContext applicationContext;
   private MockWebServiceClient mockClient;

   @BeforeEach
   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));
   }	
}

Running the Project

Once you are done with creating source and configuration files, Run the test cases using maven test

[INFO] Scanning for projects...
[INFO] 
[INFO] [1m-----------------< [0;36mcom.tutorialspoint.hr:leaveservice[0;1m >-----------------[m
[INFO] [1mBuilding leaveService Spring-WS Application 0.0.1-SNAPSHOT[m
[INFO]   from pom.xml
[INFO] [1m--------------------------------[ war ]---------------------------------[m
[INFO] 
[INFO] [1m--- [0;32mresources:3.3.1:resources[m [1m(default-resources)[m @ [36mleaveservice[0;1m ---[m
[INFO] Copying 2 resources from src\main\resources to target\classes
[INFO] 
[INFO] [1m--- [0;32mcompiler:3.13.0:compile[m [1m(default-compile)[m @ [36mleaveservice[0;1m ---[m
[INFO] Nothing to compile - all classes are up to date.
[INFO] 
[INFO] [1m--- [0;32mresources:3.3.1:testResources[m [1m(default-testResources)[m @ [36mleaveservice[0;1m ---[m
[INFO] skip non existing resourceDirectory C:\Users\mahes\eclipse-workspace\leaveservice\src\test\resources
[INFO] 
[INFO] [1m--- [0;32mcompiler:3.13.0:testCompile[m [1m(default-testCompile)[m @ [36mleaveservice[0;1m ---[m
[INFO] Nothing to compile - all classes are up to date.
[INFO] 
[INFO] [1m--- [0;32msurefire:3.3.0:test[m [1m(default-test)[m @ [36mleaveservice[0;1m ---[m
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.tutorialspoint.ws.[1mCustomerEndPointTest[m
Oct 19, 2025 3:05:18 PM org.springframework.ws.soap.addressing.server.AbstractAddressingEndpointMapping afterPropertiesSet
INFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
Oct 19, 2025 3:05:19 PM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet
INFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
[INFO] [1;32mTests run: [0;1;32m1[m, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.090 s -- in com.tutorialspoint.ws.[1mCustomerEndPointTest[m
[INFO] 
[INFO] Results:
[INFO] 
[INFO] [1;32mTests run: 1, Failures: 0, Errors: 0, Skipped: 0[m
[INFO] 
[INFO] [1m------------------------------------------------------------------------[m
[INFO] [1;32mBUILD SUCCESS[m
[INFO] [1m------------------------------------------------------------------------[m
[INFO] Total time:  3.246 s
[INFO] Finished at: 2025-10-19T15:05:19+05:30
[INFO] [1m------------------------------------------------------------------------[m
Advertisements