1. Overview
1.概述
In this tutorial, we’ll show how to run JUnit tests directly from Java code – there are scenarios where this option comes in handy.
在本教程中,我们将展示如何直接从Java代码中运行JUnit测试–在一些场景中,这个选项会很方便。
If you are new to JUnit, or if you want to upgrade to JUnit 5, you can check some of many tutorials we have on the topic.
如果你是JUnit的新手,或者你想升级到JUnit 5,你可以查看我们关于该主题的一些许多教程。
2. Maven Dependencies
2.Maven的依赖性
We’ll need a couple of basic dependencies to run both JUnit 4 and JUnit 5 tests:
我们需要一些基本的依赖关系来运行JUnit 4和JUnit 5测试。
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
// for JUnit 4
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
The latest versions of JUnit 4, JUnit 5, and JUnit Platform Launcher can be found on Maven Central.
JUnit 4, JUnit 5和JUnit Platform Launcher的最新版本可在Maven Central找到。
3. Running JUnit 4 Tests
3.运行JUnit 4测试
3.1. Test Scenario
3.1.测试场景
For both JUnit 4 and JUnit 5, we’ll set up a few “placeholder” test classes which will be enough to demonstrate our examples:
对于JUnit 4和JUnit 5,我们将设置一些 “占位符 “测试类,这足以证明我们的例子。
public class FirstUnitTest {
@Test
public void whenThis_thenThat() {
assertTrue(true);
}
@Test
public void whenSomething_thenSomething() {
assertTrue(true);
}
@Test
public void whenSomethingElse_thenSomethingElse() {
assertTrue(true);
}
}
public class SecondUnitTest {
@Test
public void whenSomething_thenSomething() {
assertTrue(true);
}
@Test
public void whensomethingElse_thenSomethingElse() {
assertTrue(true);
}
}
When using JUnit 4, we create test classes adding @Test annotation to every test method.
当使用JUnit 4时,我们创建测试类时,在每个测试方法中添加@Test注解。
We can also add other useful annotations, such as @Before or @After, but that’s not in the scope of this tutorial.
我们还可以添加其他有用的注释,如@Before或@After,但这不在本教程的范围内。
3.2. Running a Single Test Class
3.2.运行单个测试类
To run JUnit tests from Java code, we can use the JUnitCore class (with an addition of TextListener class, used to display the output in System.out):
为了从Java代码中运行JUnit测试,我们可以使用JUnitCore类(增加了TextListener类,用于在System.out中显示输出)。
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(FirstUnitTest.class);
On the console, we’ll see a very simple message indicating successful tests:
在控制台,我们会看到一个非常简单的信息,表明测试成功。
Running one test class:
..
Time: 0.019
OK (2 tests)
3.3. Running Multiple Test Classes
3.3.运行多个测试类
If we want to specify multiple test classes with JUnit 4, we can use the same code as for a single class, and simply add the additional classes:
如果我们想用JUnit 4指定多个测试类,我们可以使用与单个类相同的代码,并简单地添加额外的类。
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
Result result = junit.run(
FirstUnitTest.class,
SecondUnitTest.class);
resultReport(result);
Note that the result is stored in an instance of JUnit’s Result class, which we’re printing out using a simple utility method:
注意,结果被存储在JUnit的Result类,我们要用一个简单的实用方法将其打印出来:
public static void resultReport(Result result) {
System.out.println("Finished. Result: Failures: " +
result.getFailureCount() + ". Ignored: " +
result.getIgnoreCount() + ". Tests run: " +
result.getRunCount() + ". Time: " +
result.getRunTime() + "ms.");
}
3.4. Running a Test Suite
3.4.运行一个测试套件
If we need to group some test classes in order to run them, we can create a TestSuite. This is just an empty class where we specify all classes using JUnit annotations:
如果我们需要对一些测试类进行分组,以便运行它们,我们可以创建一个TestSuite。这只是一个空的类,在这里我们使用JUnit注解指定所有的类。
@RunWith(Suite.class)
@Suite.SuiteClasses({
FirstUnitTest.class,
SecondUnitTest.class
})
public class MyTestSuite {
}
To run these tests, we’ll again use the same code as before:
为了运行这些测试,我们将再次使用与之前相同的代码。
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
Result result = junit.run(MyTestSuite.class);
resultReport(result);
3.5. Running Repeated Tests
3.5.运行重复的测试
One of the interesting features of JUnit is that we can repeat tests by creating instances of RepeatedTest. This can be really helpful when we’re testing random values, or for performance checks.
JUnit的一个有趣的特点是,我们可以通过创建RepeatedTest的实例来重复测试。当我们测试随机值或进行性能检查时,这可能非常有用。
In the next example, we’ll run the tests from MergeListsTest five times:
在下一个例子中,我们将运行MergeListsTest的测试五次。
Test test = new JUnit4TestAdapter(FirstUnitTest.class);
RepeatedTest repeatedTest = new RepeatedTest(test, 5);
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
junit.run(repeatedTest);
Here, we’re using JUnit4TestAdapter as a wrapper for our test class.
在这里,我们使用JUnit4TestAdapter作为我们测试类的包装。
We can even create suites programmatically, applying repeated testing:
我们甚至可以以编程方式创建套件,应用重复测试。
TestSuite mySuite = new ActiveTestSuite();
JUnitCore junit = new JUnitCore();
junit.addListener(new TextListener(System.out));
mySuite.addTest(new RepeatedTest(new JUnit4TestAdapter(FirstUnitTest.class), 5));
mySuite.addTest(new RepeatedTest(new JUnit4TestAdapter(SecondUnitTest.class), 3));
junit.run(mySuite);
4. Running JUnit 5 Tests
4.运行JUnit 5测试
4.1. Test Scenario
4.1.测试场景
With JUnit 5, we’ll use the same sample test classes as for the previous demo – FirstUnitTest and SecondUnitTest, with some minor differences due to a different version of JUnit framework, like the package for @Test and assertion methods.
在JUnit 5中,我们将使用与之前的演示相同的测试样本类–FirstUnitTest和SecondUnitTest,由于JUnit框架的版本不同,有一些小的差别,比如@Test的包和断言方法。
4.2. Running Single Test Class
4.2.运行单个测试类
To run JUnit 5 tests from Java code, we’ll set up an instance of LauncherDiscoveryRequest. It uses a builder class where we must set package selectors and testing class name filters, to get all test classes that we want to run.
为了从Java代码中运行JUnit 5测试,我们将设置一个LauncherDiscoveryRequest的实例。它使用一个构建器类,我们必须设置包选择器和测试类名称过滤器,以获得我们想要运行的所有测试类。
This request is then associated with a launcher and, before executing the tests, we’ll also set up a test plan and an execution listener.
然后这个请求与一个启动器相关联,在执行测试之前,我们还要设置一个测试计划和一个执行监听器。
Both of these will offer information about the tests to be executed and of the results:
这两者都将提供关于要执行的测试和结果的信息。
public class RunJUnit5TestsFromJava {
SummaryGeneratingListener listener = new SummaryGeneratingListener();
public void runOne() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(FirstUnitTest.class))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
// main method...
}
4.3. Running Multiple Test Classes
4.3.运行多个测试类
We can set selectors and filters to the request to run multiple test classes.
我们可以对请求设置选择器和过滤器,以运行多个测试类。
Let’s see how we can set package selectors and testing class name filters, to get all test classes that we want to run:
让我们看看如何设置包选择器和测试类名称过滤器,以获得我们想要运行的所有测试类。
public void runAll() {
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectPackage("com.baeldung.junit5.runfromjava"))
.filters(includeClassNamePatterns(".*Test"))
.build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
}
4.4. Test Output
4.4.测试输出
In the main() method, we call our class, and we also use the listener to get the result details. This time the result is stored as a TestExecutionSummary.
在main()方法中,我们调用我们的类,并且我们也使用监听器来获取结果的细节。这一次,结果被存储为TestExecutionSummary。
The simplest way to extract its info merely is by printing to a console output stream:
最简单的方法是通过打印到控制台输出流来提取其信息。
public static void main(String[] args) {
RunJUnit5TestsFromJava runner = new RunJUnit5TestsFromJava();
runner.runAll();
TestExecutionSummary summary = runner.listener.getSummary();
summary.printTo(new PrintWriter(System.out));
}
This will give us the details of our test run:
这将为我们提供测试运行的细节。
Test run finished after 177 ms
[ 7 containers found ]
[ 0 containers skipped ]
[ 7 containers started ]
[ 0 containers aborted ]
[ 7 containers successful ]
[ 0 containers failed ]
[ 10 tests found ]
[ 0 tests skipped ]
[ 10 tests started ]
[ 0 tests aborted ]
[ 10 tests successful ]
[ 0 tests failed ]
5. Conclusion
5.结论
In this article, we’ve shown how to run JUnit tests programmatically from Java code, covering JUnit 4 as well as the recent JUnit 5 version of this testing framework.
在这篇文章中,我们展示了如何从Java代码中以编程方式运行JUnit测试,涵盖了JUnit 4以及这个测试框架的最新JUnit 5版本。
As always, the implementation of the examples shown here is available over on GitHub for both the JUnit 5 examples, as well as JUnit 4.
一如既往,这里显示的示例的实现可以在GitHub上获得,包括JUnit 5 示例以及JUnit 4。