maven cxf-codegen-plugin插件测试webservice

测试webservice时,我们一般要生成webservice本地类,Apache-cxf包自带一个wsdl2java命令行下的工具,使用方法见https://www.yeetrack.com/?p=510,其实更方便的方法是使用maven cxf-codegen-plugin插件,这个插件允许我们在执行mvn installmvn test等命令时,动态生成webservice本地类,防止webservice接口已经变动,我们仍旧用旧的本地类进行测试。cxf-codegen-plugin官方文档地址:http://cxf.apache.org/docs/maven-cxf-codegen-plugin-wsdl-to-java.html,下面是自己的一个例子:

仍旧使用查询号码归属地的webservice接口,http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl

配置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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.yeetrack</groupId>
<artifactId>webservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>webservice</name>
<url>http://maven.apache.org</url>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
    </dependency>                   
 </dependencies>
 <build>
 <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.7.2</version>
            <configuration>
                <forkMode>once</forkMode>
                <argLine>-Dfile.encoding=UTF-8</argLine>
                <systemProperties>
                    <property>
                        <name>net.sourceforge.cobertura.datafile</name>
                        <value>target/cobertura/cobertura.ser</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-codegen-plugin</artifactId>
            <version>2.7.3</version>
            <executions>
                <execution>
                    <id>generate-sources</id>
                    <phase>generate-sources</phase>
                    <configuration>
                        <sourceRoot>${project.build.sourceDirectory}</sourceRoot>
                        <encoding>UTF-8</encoding>
                        <wsdlOptions>
                            <wsdlOption>
                                <wsdl>http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl</wsdl>
                            </wsdlOption>
                        </wsdlOptions>
                    </configuration>
                    <goals>
                        <goal>wsdl2java</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </build>
</project>

编写自己的testCase:

package com.yeetrack.webservice;

import org.testng.annotations.Test;

import cn.com.webxml.MobileCodeWS;
import cn.com.webxml.MobileCodeWSSoap;

/**
* @author youthflies
* yeetrack.com
*/
public class AppTest 
{
    @Test
    public void mobileInfoTest()
    {
        //实例化接口实现类
        MobileCodeWS mobileCodeWS = new MobileCodeWS();
        //实例化接口
        MobileCodeWSSoap mobileCodeWSSoap = mobileCodeWS.getMobileCodeWSSoap();
        //调用接口中的方法
        System.out.println(mobileCodeWSSoap.getMobileCodeInfo("13898767654", ""));
    }
}  

将testCase放在test代码文件夹内,直接执行mvn install或者mvn test即可,当然也可以放在hudson和jenkins上执行。

apache-cxf测试webservice添加header信息

版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=528