Spring服务器(单元测试)

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

我们先来看看完整的目录结构 -

请参考以下步骤实现单元测试 -

第1步 : 更新在Spring WS编写服务器章节中创建的项目:countryService。 添加一个src/test/java文件夹。
第2步 : 在 - src/test/java/com/zyiz/ws文件夹下创建一个类:CustomerEndPointTest.java,然后更新pom.xml,如下所述。

第3步 :在src/main/resources子文件夹下添加spring-context.xml

第4步 :最后一步是为所有源文件和配置文件创建内容并按照下面的说明测试应用程序。

文件:pom.xml -

<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.zyiz</groupId>
    <artifactId>countryService</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>countryService Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <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>
    <build>
        <finalName>countryService</finalName>
        <defaultGoal>compile</defaultGoal>
    </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.zyiz"/>
   <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.zyiz.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://www.zyiz/schemas'>"
                + "<name>United States</name>" + "</getCountryRequest>");
        Source responsePayload = new StringSource("<getCountryResponse xmlns='http://www.zyiz/schemas'>"
                + "<country>" + "<name>United States</name>" + "<population>46704314</population>"
                + "<capital>Washington</capital>" + "<currency>USD</currency>" + "</country>"
                + "</getCountryResponse>");
        mockClient.sendRequest(withPayload(requestPayload)).andExpect(payload(responsePayload));
    }
}

构建项目

打开Eclipse,在项目名称上右键,选择菜单:Run As… ,然后选择:Maven test , 运行结果如下 -

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building countryService Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ countryService ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 8 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 (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\eclipse-workspace\countryService\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: D:\eclipse-workspace\countryService\countryService\target\surefire-reports
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom (3 KB at 1.3 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (3 KB at 6.5 KB/sec)
[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar
[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 KB at 58.1 KB/sec)

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.zyiz.ws.CustomerEndPointTest
五月 30, 2018 9:16:39 上午 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: @TestExecutionListeners is not present for class [class com.zyiz.ws.CustomerEndPointTest]: using defaults.
五月 30, 2018 9:16:39 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-context.xml]
五月 30, 2018 9:16:39 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@6833ce2c: startup date [Wed May 30 09:16:39 CST 2018]; root of context hierarchy
五月 30, 2018 9:16:40 上午 org.springframework.ws.soap.addressing.server.AnnotationActionEndpointMapping afterPropertiesSet
信息: Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
五月 30, 2018 9:16:40 上午 org.springframework.ws.soap.saaj.SaajSoapMessageFactory afterPropertiesSet
信息: Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
五月 30, 2018 9:16:41 上午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@6833ce2c: startup date [Wed May 30 09:16:39 CST 2018]; root of context hierarchy
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.167 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.695 s
[INFO] Finished at: 2018-05-30T09:16:41+08:00
[INFO] Final Memory: 13M/138M
[INFO] ------------------------------------------------------------------------

上一篇:Spring编写服务器

下一篇:Spring编写客户端

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程