- Mastering Software Testing with JUnit 5
- Boni García
- 377字
- 2021-07-02 15:34:26
Test execution in JUnit 3
JUnit 3 allows to run test cases by means of Java applications called test runners. JUnit 3.8.2 provides three different test runners out of the box: two graphical (Swing and AWT based) and one textual that can be used from the command line. The JUnit framework provides separate class loaders for each test, in order to avoid side effects among tests.
It is a common practice that build tools (such as Ant or Maven) and Integrated Development Environments -IDE- (such as Eclipse and IntelliJ) implement its own JUnit test runner.
The following image shows what the previous test looks like when we use the JUnit Swing runner, and also when we use Eclipse to run the same test case.

When a test is not succeeded in JUnit, it can be for two reasons: a failure or an error. On the one hand, a failure is caused by an assertion (Assert class) which is not meet. On the other hand, an error is an unexpected condition not expected by the test, such as a conventional exception in the software under test.
Another important contribution of JUnit 3 is the concept of the test suite, which is a convenient way to group tests that are related. Test suites are implemented by means of the JUnit class junit.framework.TestSuite. This class, in the same way as TestCase, implements the framework interface junit.framework.Test.
A diagram containing the main classes and methods of JUnit 3 is depicted as follows:

The following snippet shows an example of the use of test suites in JUnit 3. In short, we can create a group of tests simply instantiating a TestSuite object, and then add single test cases using the method addTestSuite():
package io.github.bonigarcia;
import junit.framework.Test;
import junit.framework.TestSuite;
public class TestAll {
public static Test suite() {
TestSuite suite = new TestSuite("All tests");
suite.addTestSuite(TestSimple.class);
suite.addTestSuite(TestMinimal.class);
return suite;
}
}
This test suite can be later executed using a test runner. For example, we could use the command-line test runner (junit.textui.TestRunner) and the command line, as follows:

- Mastering Zabbix(Second Edition)
- TestNG Beginner's Guide
- Linux網(wǎng)絡程序設計:基于龍芯平臺
- Spring實戰(zhàn)(第5版)
- 深入理解Elasticsearch(原書第3版)
- Python Data Science Cookbook
- Java Web從入門到精通(第2版)
- MySQL 8從零開始學(視頻教學版)
- 30天學通C#項目案例開發(fā)
- .NET 4.0面向?qū)ο缶幊搪劊簯闷?/a>
- Learning D
- FusionCharts Beginner’s Guide:The Official Guide for FusionCharts Suite
- Building a Media Center with Raspberry Pi
- ASP.NET jQuery Cookbook(Second Edition)
- JavaScript設計模式與開發(fā)實踐