JUnit - API
JUnit中最重要的包是junit.framework,它包含所有核心类。 一些重要的类如下 −
| Sr.No. | 类名 | 功能 |
|---|---|---|
| 1 | Assert | 一组断言方法。 |
| 2 | TestCase | 测试用例定义运行多个测试的装置。 |
| 3 | TestResult | TestResult 收集执行测试用例的结果。 |
| 4 | TestSuite | TestSuite 是测试的组合。 |
Assert 类
以下是 org.junit.Assert 类的声明 −
public class Assert extends java.lang.Object
此类提供了一组对于编写测试有用的断言方法。 仅记录失败的断言。 Assert类的一些重要方法如下 −
| Sr.No. | 方法和描述 |
|---|---|
| 1 | void assertEquals(boolean expected, boolean actual) 检查两个基元/对象是否相等。 |
| 2 | void assertFalse(boolean condition) 检查条件是否为假。 |
| 3 | void assertNotNull(Object object) 检查对象是否为 null。 |
| 4 | void assertNull(Object object) 检查对象是否为 null。 |
| 5 | void assertTrue(boolean condition) 检查条件是否为真。 |
| 6 | void fail() 测试失败且没有消息。 |
让我们在示例中使用上述一些方法。 在 C:\>JUNIT_WORKSPACE 中创建名为 TestJunit1.java 的 java 类文件。
import org.junit.Test;
import static org.junit.Assert.*;
public class TestJunit1 {
@Test
public void testAdd() {
//test data
int num = 5;
String temp = null;
String str = "Junit is working fine";
//check for equality
assertEquals("Junit is working fine", str);
//check for false condition
assertFalse(num > 6);
//check for not null value
assertNotNull(temp);
}
}
接下来,在 C:\>JUNIT_WORKSPACE 中创建一个名为 TestRunner1.java 的 java 类文件来执行测试用例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner1 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit1.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用 javac 编译测试用例和测试运行器类。
C:\JUNIT_WORKSPACE>javac TestJunit1.java TestRunner1.java
现在运行测试运行程序,它将运行提供的测试用例类中定义的测试用例。
C:\JUNIT_WORKSPACE>java TestRunner1
验证输出。
true
TestCase 类
以下是 org.junit.TestCase 类的声明 −
public abstract class TestCase extends Assert implements Test
测试用例定义了运行多个测试的装置。 TestCase类的一些重要方法如下 −
| Sr.No. | 方法和描述 |
|---|---|
| 1 | int countTestCases() 计算run(TestResult result)执行的测试用例的数量。 |
| 2 | TestResult createResult() 创建一个默认的 TestResult 对象。 |
| 3 | String getName() 获取测试用例的名称。 |
| 4 | TestResult run() 运行此测试的便捷方法,使用默认的 TestResult 对象收集结果。 |
| 5 | void run(TestResult result) 运行测试用例并将结果收集到 TestResult 中。 |
| 6 | void setName(String name) 设置测试用例的名称。 |
| 7 | void setUp() 设置夹具,例如打开网络连接。 |
| 8 | void tearDown() 拆除设备,例如,关闭网络连接。 |
| 9 | String toString() 返回测试用例的字符串表示形式。 |
让我们在示例中使用上述一些方法。 在 C:\>JUNIT_WORKSPACE 中创建一个名为 TestJunit2.java 的 java 类文件。
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
public class TestJunit2 extends TestCase {
protected double fValue1;
protected double fValue2;
@Before
public void setUp() {
fValue1 = 2.0;
fValue2 = 3.0;
}
@Test
public void testAdd() {
//count the number of test cases
System.out.println("No of Test Case = "+ this.countTestCases());
//test getName
String name = this.getName();
System.out.println("Test Case Name = "+ name);
//test setName
this.setName("testNewAdd");
String newName = this.getName();
System.out.println("Updated Test Case Name = "+ newName);
}
//tearDown used to close the connection or clean up activities
public void tearDown( ) {
}
}
接下来,在 C:\>JUNIT_WORKSPACE 中创建一个名为 TestRunner2.java 的 java 类文件来执行测试用例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner2 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit2.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用 javac 编译测试用例和测试运行器类。
C:\JUNIT_WORKSPACE>javac TestJunit2.java TestRunner2.java
现在运行测试运行程序,它将运行提供的测试用例类中定义的测试用例。
C:\JUNIT_WORKSPACE>java TestRunner2
验证输出。
No of Test Case = 1 Test Case Name = testAdd Updated Test Case Name = testNewAdd true
TestResult 类
以下是 org.junit.TestResult 类的声明−
public class TestResult extends Object
TestResult 收集执行测试用例的结果。 它是收集参数模式的一个实例。 测试框架区分失败和错误。 可以预见失败并通过断言进行检查。 错误是不可预见的问题,例如 ArrayIndexOutOfBoundsException。 TestResult类的一些重要方法如下 −
| Sr.No. | 方法和描述 |
|---|---|
| 1 | void addError(Test test, Throwable t) 将错误添加到错误列表中。 |
| 2 | void addFailure(Test test, AssertionFailedError t) 将故障添加到故障列表中。 |
| 3 | void endTest(Test test) 通知测试已完成的结果。 |
| 4 | int errorCount() 获取检测到的错误数。 |
| 5 | Enumeration<TestFailure> errors() 返回错误的枚举。 |
| 6 | int failureCount() 获取检测到的故障数量。 |
| 7 | void run(TestCase test) 运行测试用例。 |
| 8 | int runCount() 获取运行测试的数量。 |
| 9 | void startTest(Test test) 通知结果,测试将开始。 |
| 10 | void stop() 标记测试运行应停止。 |
在 C:\>JUNIT_WORKSPACE 中创建一个名为 TestJunit3.java 的 java 类文件。
import org.junit.Test;
import junit.framework.AssertionFailedError;
import junit.framework.TestResult;
public class TestJunit3 extends TestResult {
// add the error
public synchronized void addError(Test test, Throwable t) {
super.addError((junit.framework.Test) test, t);
}
// add the failure
public synchronized void addFailure(Test test, AssertionFailedError t) {
super.addFailure((junit.framework.Test) test, t);
}
@Test
public void testAdd() {
// add any test
}
// Marks that the test run should stop.
public synchronized void stop() {
//stop the test here
}
}
接下来,在 C:\>JUNIT_WORKSPACE 中创建一个名为 TestRunner3.java 的 java 类文件来执行测试用例。
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner3 {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit3.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
使用 javac 编译测试用例和测试运行器类。
C:\JUNIT_WORKSPACE>javac TestJunit3.java TestRunner3.java
现在运行测试运行程序,它将运行提供的测试用例类中定义的测试用例。
C:\JUNIT_WORKSPACE>java TestRunner3
验证输出。
true
TestSuite 类
以下是 org.junit.TestSuite 类的声明:
public class TestSuite extends Object implements Test
TestSuite 是测试的组合。 它运行一组测试用例。 TestSuite类的一些重要方法如下 −
| Sr.No. | 方法和描述 |
|---|---|
| 1 | void addTest(Test test) 向套件添加测试。 |
| 2 | void addTestSuite(Class<? extends TestCase> testClass) 将给定类中的测试添加到套件中。 |
| 3 | int countTestCases() 计算此测试将运行的测试用例的数量。 |
| 4 | String getName() 返回套件的名称。 |
| 5 | void run(TestResult result) 运行测试并将结果收集到 TestResult 中。 |
| 6 | void setName(String name) 设置套件的名称。 |
| 7 | Test testAt(int index) 返回给定索引处的测试。 |
| 8 | int testCount() 返回该套件中的测试数量。 |
| 9 | static Test warning(String message) 返回将失败并记录警告消息的测试。 |
在 C:\>JUNIT_WORKSPACE 中创建一个名为 JunitTestSuite.java 的 java 类文件来创建测试套件。
import junit.framework.*;
public class JunitTestSuite {
public static void main(String[] a) {
// add the test's in the suite
TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class, TestJunit3.class );
TestResult result = new TestResult();
suite.run(result);
System.out.println("Number of test cases = " + result.runCount());
}
}
使用 javac 编译测试套件类。
C:\JUNIT_WORKSPACE>javac JunitTestSuite.java
现在运行测试套件。
C:\JUNIT_WORKSPACE>java JunitTestSuite
验证输出。
No of Test Case = 1 Test Case Name = testAdd Updated Test Case Name = testNewAdd Number of test cases = 3

