Maven學習三 使用junit測試maven project
阿新 • • 發佈:2018-12-19
每個開發人員都會對自己的程式碼進行自定義的測試,可以是把專案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?
- <?xmlversion="1.0"encoding="UTF-8"?>
- <projectxmlns="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>
- 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");
- }
- }
- 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 :