第3章:Maven使用入門/3.3 編寫測試程式碼
- 編寫測試程式碼
- 在約定src/test/java下新建測試程式碼類
- `package com.wys.myapp.helloworld;
import org.junit.Test;
import static org.junit.Assert.*;
public class HelloWorldErrTest {
@Test
public void SayHello(){
HelloWorld helloWorld = new HelloWorld();
String restult = helloWorld.SayHello();
assertEquals("hello world123",restult);
}
}`
- `package com.wys.myapp.helloworld;
import org.junit.Test;
import static org.junit.Assert.*;
public class HelloWorldTest {
@Test
public void SayHello(){
HelloWorld helloWorld = new HelloWorld();
String restult = helloWorld.SayHello();
assertEquals("hello world",restult);
}
}`
-
執行mvn clean test:會自動執行src/test/java下面所有的main方法完成測試案例執行,如果有報錯則丟擲異常並且終止測試
[D:\MavenTest\HelloWord]$ mvn clean test
[INFO] Scanning for projects...T E S T S
Running com.wys.myapp.helloworld.HelloWorldTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.062 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.225 s
[INFO] Finished at: 2018-08-02T14:29:58+08:00
[INFO] Final Memory: 16M/180M
[INFO] ------------------------------------------------------------------------
[D:\MavenTest\HelloWord]$