单元测试是对最小单元即方法的测试,要隔离对他模块的依赖,一般采用stub和mock两种方式
集成测试是对功能的测试,对于大部分web模块来说需要启动web容器,进行集成测试
maven生命周期中已经包含测试(test)和集成测试(integration-test),但未对两种测试代码做区分,需要自己解决启动web容器和代码区分问题。
首先配置maven jetty插件在集成测试阶段自动启动
org.mortbay.jetty
maven-jetty-plugin
6.1.10
9966
stop-jetty-for-it
/SDKAuth
10
${basedir}/src/test/resources/jetty-env.xml
start-jetty
pre-integration-test
run
true
stop-jetty
post-integration-test
stop
然后测试代码进行单元测试和集成测试区分,一般有两种方式:
一是使用maven profile,通过profile区分
二是根据生命周期,配置maven surefire 插件不同生命周期的includes或/exclueds属性
我使用的方式二,在src/test/java目中把单元测试代码放在unit包,集成测试代码放在integration包,具体配置如下:
org.apache.maven.plugins
maven-surefire-plugin
2.9
true
run-integration-test
integration-test
test
false
**/integration/**/*.java
run-test
test
test
false
**/unit/**/*.java
org.apache.maven.plugins
maven-war-plugin
2.0
WEB-INF/lib
可以参考[url]http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing[/url]
运行集成测试命令 : mvn integration-test
[quote]
2012-05-16 15:36:44.293::INFO: Started
[email protected]: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
[/quote]
[img]http://my.iteye.com/feed?subscription%5Bsubscribed_user_name%5D=bloodwolf-china[/img]