maven执行单元和集成测试

    单元测试是对最小单元即方法的测试,要隔离对他模块的依赖,一般采用stub和mock两种方式。

1.集成测试

    集成测试是对功能的测试,对于大部分web模块来说需要启动web容器,进行集成测试 

    maven生命周期中已经包含测试(test)和集成测试(integration-test),但未对两种测试代码做区分,需要自己解决启动web容器和代码区分问题。 

2.设置maven jetty插件自启动

    首先配置maven jetty插件在集成测试阶段自动启动 
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
< plugin >
                 < groupId >org.mortbay.jetty</ groupId >
                 < artifactId >maven-jetty-plugin</ artifactId >
                 < version >6.1.10</ version >
                 < configuration >
                     < stopPort >9966</ stopPort >
                     < stopKey >stop-jetty-for-it</ stopKey >
                     < contextPath >/SDKAuth</ contextPath >
                     < scanIntervalSeconds >10</ scanIntervalSeconds >
                     < jettyEnvXml >${basedir}/src/test/resources/jetty-env.xml</ jettyEnvXml >
                 </ configuration >
                 < executions >
                     < execution >
                         < id >start-jetty</ id >
                         <!--集成测试前启动jetty-->
                         < phase >pre-integration-test</ phase >
                         < goals >
                             < goal >run</ goal >
                         </ goals >
                         < configuration >
                             < daemon >true</ daemon >
                         </ configuration >
                     </ execution >
                     < execution >
                         < id >stop-jetty</ id >
                         <!--集成测试结束停止jetty-->
                         < phase >post-integration-test</ phase >
                         < goals >
                             < goal >stop</ goal >
                         </ goals >
                     </ execution >
                 </ executions >
             </ plugin >

3.集成测试两种方式

然后测试代码进行单元测试和集成测试区分,一般有两种方式: 
   一是使用maven profile,通过profile区分 
   二是根据生命周期,配置maven surefire 插件不同生命周期的includes或/exclueds属性 
   我使用的方式二,在src/test/java目中把单元测试代码放在unit包,集成测试代码放在integration包,具体配置如下: 

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>run-integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <includes>
                                <include>**/integration/**/*.java</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>run-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <includes>
                                <include>**/unit/**/*.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.0</version>
                <configuration>
                    <dependentWarExcludes>WEB-INF/lib</dependentWarExcludes>
                </configuration>
            </plugin>
        </plugins>
可以参考 http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing  
3. 运行集成测试命令 : 

?
1
mvn integration-test



4.结果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2012-05-16 15:36:44.293::INFO:  Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server
[INFO] Starting scanner at interval of 10 seconds.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.346 sec
 
Results :
 
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值