- 在约定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]$
原文地址:http://blog.51cto.com/12074015/2331239