在maven project-01項目中引入junit插件並不困難,只需要在pom.xml中配置好依賴包即可(IDE環境下,依賴項會自動生成)。剩下的測試代碼的編寫並沒有任何不同。另外,test類文件需要按照/src/main/java/的文件結構(main改爲test)。
test文件夾結構如下圖:
還需要修改pom.xml文件,增加dependencies項,改後的pom.xml內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<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>jizg.study.maven.hello</groupId>
<artifactId>hello-first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
接下來在/src/test/java/jizg/study/maven/hello 文件夾下創建TestHello.java文件,這裏需要注意,test的包結構可以自定義,要注意必備的路徑爲/src/test/java/,TestHello.java內容如下:
package jizg.study.maven.hello;
import org.junit.*;
import static junit.framework.Assert.*;
import jizg.study.maven.hello.*;
public class TestHello{
@Test
public void testHello(){
Hello h = new Hello();
assertEquals(h.sayHello("jizg"),"hello :jizg");
}
}
最後,改好pom.xml和test類文件之後,可以輸入mvn test命令,這會重新把項目build出來,並且輸出TestHello.java中的test信息。
控制檯輸出如下:
D:\study\maven\01>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building hello-first 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-firs
t ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\study\maven\01\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-first -
--
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he
llo-first ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e
. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\study\maven\01\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello
-first ---
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil
d is platform dependent!
[INFO] Compiling 1 source file to D:\study\maven\01\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-first ---
[INFO] Surefire report directory: D:\study\maven\01\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running test.TestHello.TestHello
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.151 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.346s
[INFO] Finished at: Wed Oct 02 18:23:23 CST 2013
[INFO] Final Memory: 9M/22M
[INFO] ------------------------------------------------------------------------
D:\study\maven\01>
細心的朋友還會發現,在project-01目錄下,新生成了一個target(項目輸出)文件夾,下面包括surefire-reports(測試結果)和編譯過後的class文件。mvn test可以很好的支持單元測試,maven下的好多命令可以完成其中奇葩怪異的任務,並且mvn 命令支持串行執行。比如,mvn install、mvn clean build等等。