English 中文(简体)
Spring WS - Unit Test Client
  • 时间:2024-12-22

Spring WS - Unit Test Cpent


Previous Page Next Page  

In this chapter, we will learn how to unit test a cpent created in the Spring WS - Writing Cpent for the web apppcation 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 CountryServiceCpentTest.java under the package com.tutorialspoint under folder SRC → Test → Java as explained in steps given below.

CountryServiceCpentTest.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.cpent.CountryServiceCpent;

pubpc class CountryServiceCpentTest {
   CountryServiceCpent cpent;
   
   @Before
   pubpc void setUp() throws Exception {
      cpent = new CountryServiceCpent();
      Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
      marshaller.setContextPath("com.tutorialspoint");
      cpent.setMarshaller(marshaller);
      cpent.setUnmarshaller(marshaller);
   }
   @Test
   pubpc void test() {
      GetCountryResponse response = cpent.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 Cpent

Let us open the command console, go to the C:MVNcountryService directory and execute the following mvn command.

C:MVNcountryService>mvn test

Maven will start building and testing the project.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building countryService Spring-WS Apppcation 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 - recompipng the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compipng 10 source files to C:MVNcountryService	argetclasses
[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:MVNcountryServicesrc	est
esour
ces
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ country
Service ---
[INFO] Changes detected - recompipng the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compipng 2 source files to C:MVNcountryService	arget	est-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ countryService ---

[INFO] Surefire report directory: C:MVNcountryService	argetsurefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.tutorialspoint.CountryServiceCpentTest
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.GenericApppcationCo
ntext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApppcationContext@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.GenericApppcationCo
ntext doClose
INFO: Closing org.springframework.context.support.GenericApppcationContext@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] ------------------------------------------------------------------------
Advertisements