开发手册 欢迎您!
软件开发者资料库

Spring WS - 单元测试服务器

Spring Web服务单元测试服务器 - 从基本到高级概念的简单简单步骤学习Java Spring Framework 4.1.6版,包括示例环境设置,控制反转(IoC),依赖注入,bean范围,bean生命周期,内部bean ,自动装配,不同模块,面向方面编程(AOP),数据库访问(JDBC),事务管理,Web MVC框架,Web流,异常处理,EJB集成和发送电子邮件等。

在本章中,我们将了解如何对使用Spring WS创建的Web应用程序服务进行单元测试.

Step描述
1更新项目countryService创建于Spring WS  - 写服务器章节.添加src/test/java文件夹.
2在-src/test/java/com/IT roof/ws文件夹下创建CustomerEndPointTest.java,然后更新POM.xml,如下所示.
3添加弹簧 - src/main/resources子文件夹下的context.xml.
4最后一步是为所有源文件和配置文件创建内容并测试应用程序,如下所述.

POM.xml

      4.0.0   com.IT屋   countryService   war   1.0-SNAPSHOT   countryService Spring-WS Application   http://www.springframework.org/spring-ws         countryService                        org.springframework.ws         spring-ws-core         2.4.0.RELEASE                           org.springframework         spring-test         2.5                           org.springframework.ws         spring-ws-test         2.4.0.RELEASE                           org.springframework         spring-tx         3.1.2.RELEASE                           jdom         jdom         1.0                           jaxen         jaxen         1.1                           wsdl4j         wsdl4j         1.6.2                           junit         junit         4.5         test         

spring-context.xml

                  

CustomerEndPointTest.java

package com.it1352.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(         ""+         "United States"+         "");      Source responsePayload = new StringSource(         "" +         "" +         "United States"+         "46704314"+         "Washington"+         "USD"+         ""+         "");      mockClient.sendRequest(withPayload(requestPayload)).andExpect(payload(responsePayload));   }}

构建项目

让我们打开命令控制台,转到到C:\ MVNN\countryService目录并执行以下mvn命令.

C:\ MVN\countryService> mvn test

Maven将开始构建并测试项目.

[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) @ countryService ---[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) @ countryService ---[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\resources[INFO][INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ countryService ---[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.it1352.ws.CustomerEndPointTestFeb 27, 2017 11:49:30 AM org.springframework.test.context.TestContextManager retrieveTestExecutionListenersINFO: @TestExecutionListeners is not present for class [class com.it1352.ws.CustomerEndPointTest]: using defaults.Feb 27, 2017 11:49:30 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [spring-context.xml]Feb 27, 2017 11:49:30 AM org.springframework.context.support.GenericApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.GenericApplicationContext@b2eddc0: startup date [Mon Feb 27 11:49:30 IST 2017]; root of context hierarchyFeb 27, 2017 11:49:31 AM org.springframework.ws.soap.addressing.server.AnnotationActionEndpointMapping afterPropertiesSetINFO: Supporting [WS-Addressing August 2004, WS-Addressing 1.0]Feb 27, 2017 11:49:31 AM org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSetINFO: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 ProtocolTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.386 secFeb 27, 2017 11:49:31 AM org.springframework.context.support.GenericApplicationContext doCloseINFO: Closing org.springframework.context.support.GenericApplicationContext@b2eddc0: startup date [Mon Feb 27 11:49:30 IST 2017]; root of context hierarchyResults :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] ------------------------------------------------------------------------