1. 程式人生 > >Maven學習三 使用junit測試maven project

Maven學習三 使用junit測試maven project

                     每個開發人員都會對自己的程式碼進行自定義的測試,可以是把專案run起來,手動點點頁面按鈕,看看操作場景和步驟點是否符合業務需要,是否存在UE上的問題。也有自己寫幾個測試類,把service類的輸入輸出是否符合標準都測試一番,這兩大類,其實就是包括了前後端的測試工作,分工各有不同。在maven中集成了junit測試包,應該說maven可以整合任何你想的到或者想不到工具外掛。     在maven project-01專案中引入junit外掛並不困難,只需要在pom.xml中配置好依賴包即可(IDE環境下,依賴項會自動生成)。剩下的測試程式碼的編寫並沒有任何不同。另外,test類檔案需要按照/src/main/java/的檔案結構(main改為test)。
    test資料夾結構如下圖:     還需要修改pom.xml檔案,增加dependencies項,改後的pom.xml內容如下:[html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    >
  4.    <modelVersion>4.0.0</modelVersion>
  5.    <groupId>jizg.study.maven.hello</groupId>
  6.    <artifactId>hello-first</artifactId>
  7.    <version>0.0.1-SNAPSHOT</version>
  8.    <dependencies>
  9.           <dependency>
  10.                <groupId>junit</groupId>
  11.                <artifactId>junit</artifactId>
  12.                <version>4.10</version>
  13.                <scope>test</scope>
  14.           </dependency>
  15.    </dependencies>
  16. </project>
   接下來在/src/test/java/jizg/study/maven/hello 資料夾下建立TestHello.java檔案,這裡需要注意,test的包結構可以自定義,要注意必備的路徑為/src/test/java/,TestHello.java內容如下:[html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. package jizg.study.maven.hello;  
  2. import org.junit.*;  
  3. import static junit.framework.Assert.*;  
  4. import jizg.study.maven.hello.*;  
  5. public class TestHello{  
  6.      @Test  
  7.      public void testHello(){       
  8.           Hello h = new Hello();  
  9.           assertEquals(h.sayHello("jizg"),"hello :jizg");  
  10.      }  
  11. }  
   最後,改好pom.xml和test類檔案之後,可以輸入mvn test命令,這會重新把專案build出來,並且輸出TestHello.java中的test資訊。控制檯輸出如下:[html] view plaincopyprint?在CODE上檢視程式碼片派生到我的程式碼片
  1. D:\study\maven\01>mvn test  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building hello-first 0.0.1-SNAPSHOT  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-firs  
  9. t ---  
  10. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e  
  11. . build is platform dependent!  
  12. [INFO] skip non existing resourceDirectory D:\study\maven\01\src\main\resources  
  13. [INFO]  
  14. [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-first -  
  15. --  
  16. [INFO] Nothing to compile - all classes are up to date  
  17. [INFO]  
  18. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he  
  19. llo-first ---  
  20. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e  
  21. . build is platform dependent!  
  22. [INFO] skip non existing resourceDirectory D:\study\maven\01\src\test\resources  
  23. [INFO]  
  24. [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello  
  25. -first ---  
  26. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil  
  27. d is platform dependent!  
  28. [INFO] Compiling 1 source file to D:\study\maven\01\target\test-classes  
  29. [INFO]  
  30. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-first ---  
  31. [INFO] Surefire report directory: D:\study\maven\01\target\surefire-reports  
  32. -------------------------------------------------------  
  33. T E S T S  
  34. -------------------------------------------------------  
  35. Running test.TestHello.TestHello  
  36. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.151 sec  
  37. Results :