项目开发的最佳实践通常要求对所有代码模块进行单元测试。
Maven 2 为您创建了一个占位符 AppTest.java 单元测试文件。
现在,将文件名改为 NumOpsTest.java,请参照清单 11 中突出显示的部分,对生成的单元测试做出改动。
为项目添加 NumOpsTest 单元测试
现在可以使用 mvn test 命令运行所有的 mojo 一直到测试阶段。
Maven 2 编译源码和单元测试。然后运行测试,同时报告成功、失败和错误的数目,如清单 12 所示:
[code][INFO] Scanning for projects…
[INFO] -------------------------------------------------------------------------
[INFO] Building Intro to Maven 2 Example 1
[INFO] task-segment: [test]
[INFO] -------------------------------------------------------------------------
[INFO] [resources:resources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:compile]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources]
[INFO] Using default encoding to copy filtered resources.
[INFO] [compiler:testCompile]
Compiling 1 source file to C:\temp\maven\NumOps\target\test-classes
[INFO] [surefire:test]
[INFO] Surefire report directory: C:\temp\maven\NumOps\target\surefire-reports
T E S T S
Running com.ibm.devworks.NumOpsTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Sat Dec 02 23:04:27 EST 2006
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------[/code]