一、概念
junit是一個專門測試的框架
集合maven進行單元測試,可批量測試類中的大量方法是否符合預期
二、作用:
單元測試:測試的內容是類中的方法,每一個方法都是獨立測試的。方法是測試的基本單位。
三、使用方法
1、pom內加入依賴
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
2、在maven中src/test/java目錄下的,創建測試程序。
推薦的創建類和方法的提示:
1、測試類的名稱:Test+待測試類名
2、測試方法的名稱:Test+方法名稱
例如:你要測試HelloMaven
創建測試類TestHelloMaven
@Test
public void testaDD(){
測試HelloMaven的add方法是否正確
}
其中testAdd叫做測試方法,定義規則:
1、方法必須是public的
2、方法必須沒有返回值
3、方法名稱自定義,推薦是Test+被測方法
4、方法上面加上注解@Test
四、舉例,Hello項目
1、新建java源程序,存放在Hello\src\main\java\com\testbk目錄下,取名HelloMaven.java
package com.testbk; import org.junit.Assert; import org.junit.Test; public class TestHelloMaven{ @Test public void testAdd(){ System.out.println("maven junit testAdd()===") HelloMaven hello = new HelloMaven(); int res = hello.add(10,20); //驗證10+20是不是30,juit提供的方法,對比結果的 //assertEquals(期望值,實際值)
Assert.assertEquals(30,res) } }
2、新建maven測試類型,存放在Hello\src\main\java\com\testbk目錄下,取名TestHelloMaven.java
package com.testbk; import org.junit.Assert; import org.junit.Test; public class TestHelloMaven{ @Test public void testAdd(){ System.out.println("maven junit testAdd()===") HelloMaven hello = new HelloMaven(); int res = hello.add(10,20); //驗證10+20是不是30,juit提供的方法,對比結果的 //assertEquals(期望值,實際值)
Assert.assertEquals(30,res) } @Test public void testAdd2(){ System.out.println("#####maven junit testAdd()2###"); HelloMaven hello = new HelloMaven(); int res = hello.add(10,20); //驗證10+20是不是30,juit提供的方法,對比結果的 //assertEquals(期望值,實際值)
Assert.assertEquals(50,res); } }
3、執行mvn clean:清理target目錄
[INFO] Scanning for projects... [INFO] [INFO] ------------------------< com.testbk:testjava >------------------------- [INFO] Building maven 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ testjava --- [INFO] Deleting D:\javaProjects\Hello\target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.196 s [INFO] Finished at: 2021-04-25T22:50:10+08:00 [INFO] ------------------------------------------------------------------------
4、執行mvn compile:編譯main/java目錄下的java為class文件,同時把class拷貝到target/classes目錄下面
[INFO] Scanning for projects... [INFO] [INFO] ------------------------< com.testbk:testjava >------------------------- [INFO] Building maven 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testjava --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testjava --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\javaProjects\Hello\target\classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.724 s [INFO] Finished at: 2021-04-25T22:54:23+08:00 [INFO] ------------------------------------------------------------------------
5、執行mvn test-compile:編譯test/java目錄下的java為class文件,同時class拷貝到target/test-classes目錄下面
[INFO] Scanning for projects... [INFO] [INFO] ------------------------< com.testbk:testjava >------------------------- [INFO] Building maven 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ testjava --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ testjava --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ testjava --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ testjava --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\javaProjects\Hello\target\test-classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.764 s [INFO] Finished at: 2021-04-25T22:55:43+08:00 [INFO] ------------------------------------------------------------------------
6、執行mvn test:查看測試結果,通過1,失敗1,並在指定目錄生成測試報告
Results : Failed tests: testAdd2(com.testbk.TestHelloMaven): expected:<50> but was:<30> Tests run: 2, Failures: 1, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.050 s [INFO] Finished at: 2021-04-25T22:57:16+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project testjava: There are test failures. [ERROR] [ERROR] Please refer to D:\javaProjects\Hello\target\surefire-reports for the individual test results. [ERROR] -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
7、修改測試代碼,並再次執行mvn test
package com.testbk; import org.junit.Assert; import org.junit.Test; public class TestHelloMaven{ @Test public void testAdd(){ System.out.println("=====maven junit testAdd()==="); HelloMaven hello = new HelloMaven(); int res = hello.add(10,20); //驗證10+20是不是30,juit提供的方法,對比結果的 //assertEquals(期望值,實際值)
Assert.assertEquals(30,res); } @Test public void testAdd2(){ System.out.println("#####maven junit testAdd()2###"); HelloMaven hello = new HelloMaven(); int res = hello.add(30,20); //驗證10+20是不是30,juit提供的方法,對比結果的 //assertEquals(期望值,實際值)
Assert.assertEquals(50,res); } }
查看運行結果,測試通過
T E S T S ------------------------------------------------------- Running com.testbk.TestHelloMaven =====maven junit testAdd()=== #####maven junit testAdd()2### Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.282 s [INFO] Finished at: 2021-04-25T22:58:41+08:00 [INFO] ------------------------------------------------------------------------