标签:core Delve message pac oca 测试 右键 equals class
除了和 IDE 继承,右键运行之外,还可以通过 main 运行测试,并且得到运行的结果数据。
官网上将这种方式叫做基于控制台的 Runner(Console based Test runner)。
<project xmlns="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>jiangbo.java.junit</groupId>
<artifactId>05-java-junit-test-runner</artifactId>
<version>1.0.0</version>
<description>基于控制台的 JUnit 示例</description>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package jiangbo.java.junit;
public class Caculator {
public static int add(int number1, int number2) {
return number1 + number2;
}
public static int subtract(int number1, int number2) {
return number1 - number2;
}
}
package jiangbo.java.junit;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
public class CaculatorTest {
@Test
public void testAdd() {
System.out.println("testAdd");
int number = Caculator.add(1, 1);
assertEquals(2, number);
}
@Test
public void testSubtract() {
System.out.println("testSubtract");
int number = Caculator.subtract(1, 1);
assertEquals(0, number);
}
}
package jiangbo.java.junit;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class CaculatorMain {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(CaculatorTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.getMessage());
}
System.out.println(result.getRunCount());
System.out.println(result.getRunTime());
System.out.println(result.wasSuccessful());
}
}
通过运行 main 方法,控制台输出:
testAdd
testSubtract
2
35
true
标签:core Delve message pac oca 测试 右键 equals class
原文地址:https://www.cnblogs.com/jiangbo44/p/13092537.html