JUnit5 @RunWith – JUnit5 @RunWith

最后修改: 2017年 11月 6日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this quick tutorial, we’ll discuss the use of the @RunWith annotation in the JUnit 5 framework.

在这个快速教程中,我们将讨论JUnit 5框架中@RunWith注释的使用。

In JUnit 5, the @RunWith annotation has been replaced by the more powerful @ExtendWith annotation.

在JUnit 5中,@RunWith注解已被更强大的@ExtendWith注解所取代

However, the @RunWith annotation can still be used in JUnit 5 for the sake of backward compatibility.

然而,为了向后兼容,@RunWith注解仍然可以在JUnit 5中使用。

2. Running Tests With a JUnit 4-Based Runner

2.用基于JUnit 4的运行器运行测试

We can run JUnit 5 tests with any older JUnit environment using the @RunWith annotation.

我们可以使用@RunWith注解在任何旧的JUnit环境下运行JUnit 5测试。

Let’s look at an example of running tests in an Eclipse version that only supports JUnit 4.

让我们看一个在只支持JUnit 4的Eclipse版本中运行测试的例子。

First, let’s create the class we’re going to test:

首先,让我们创建我们要测试的类。

public class Greetings {
    public static String sayHello() {
        return "Hello";
    }  
}

Then we’ll create this plain JUnit 5 test:

然后我们将创建这个普通的JUnit 5测试。

public class GreetingsTest {
    @Test
    void whenCallingSayHello_thenReturnHello() {
        assertTrue("Hello".equals(Greetings.sayHello()));
    }
}

Finally, let’s add this annotation so we’re able to run the test:

最后,让我们添加这个注解,这样我们就能运行测试了。

@RunWith(JUnitPlatform.class)
public class GreetingsTest {
    // ...
}

The JUnitPlatform class is a JUnit 4 based runner that lets us run JUnit 4 tests on the JUnit Platform.

JUnitPlatform类是一个基于JUnit 4的运行器,让我们在JUnit平台上运行JUnit 4测试。

Let’s keep in mind that JUnit 4 doesn’t support all the features of the new JUnit Platform, so this runner has limited functionality.

让我们记住,JUnit 4并不支持新的JUnit平台的所有功能,所以这个运行器的功能有限。

If we check the results of the test in Eclipse, we can see that a JUnit 4 runner was used:

如果我们在Eclipse中检查测试结果,我们可以看到使用的是JUnit 4 runner。

junit4 test

3. Running Tests in a JUnit 5 Environment

3.在JUnit 5环境中运行测试

Now let’s run the same test in an Eclipse version that supports JUnit 5. In this case, we don’t need the @RunWith annotation anymore, and we can write the test without a runner:

现在让我们在支持 JUnit 5 的 Eclipse 版本中运行同一个测试。在这种情况下,我们不再需要 @RunWith 注解,我们可以不使用运行器来编写测试。

public class GreetingsTest {
    @Test
    void whenCallingSayHello_thenReturnHello() {
        assertTrue("Hello".equals(Greetings.sayHello()));
    }
}

The test results show that we’re now using the JUnit 5 runner:

测试结果显示,我们现在使用的是JUnit 5运行器。

junit5 test

4. Migrating From a JUnit 4-Based Runner

4.从基于JUnit 4的运行器迁移

Now let’s migrate a test that uses a JUnit 4 based runner to JUnit 5.

现在让我们把一个使用基于JUnit 4的运行器的测试迁移到JUnit 5。

We’re going to use a Spring test as an example:

我们将使用一个Spring测试作为例子。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
    // ...
}

If we want to migrate this test to JUnit 5, we need to replace the @RunWith annotation with the new @ExtendWith:

如果我们想把这个测试迁移到JUnit 5,我们需要用新的@ExtendWith注释替换@RunWith

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = { SpringTestConfiguration.class })
public class GreetingsSpringTest {
    // ...
}

The SpringExtension class is provided by Spring 5, and integrates the Spring TestContext Framework into JUnit 5. The @ExtendWith annotation accepts any class that implements the Extension interface.

SpringExtension类是由Spring 5提供的,它将Spring TestContext框架整合到JUnit 5。@ExtendWith注解接受任何实现Extension接口的类。

5. Conclusion

5.结论

In this brief article, we covered the use of JUnit 4’s @RunWith annotation in the JUnit 5 framework.

在这篇简短的文章中,我们介绍了JUnit 4的@RunWith注解在JUnit 5框架中的使用。

The full source code for this article is available over on GitHub.

本文的完整源代码可在GitHub上获得