JUnit5 Programmatic Extension Registration with @RegisterExtension – 用@RegisterExtension注册JUnit5程序性扩展

最后修改: 2018年 6月 8日

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

1. Overview

1.概述

JUnit 5 provides multiple methods for registering extensions. For an overview of some of these methods, refer to our Guide to JUnit 5 Extensions.

JUnit 5 提供了多种用于注册扩展的方法。有关其中一些方法的概述,请参考我们的JUnit 5扩展指南

In this quick tutorial, we’ll focus on programmatic registration of JUnit 5 extensions, using the @RegisterExtension annotation.

在这个快速教程中,我们将关注JUnit 5扩展的编程注册,使用@RegisterExtension注解。

2. @RegisterExtension

2.@RegisterExtension

We can apply this annotation to fields in test classes. One advantage of this method is that we can access the extension as an object in the test content directly. 

我们可以将这个注解应用于测试类中的字段。这种方法的一个优点是,我们可以在测试内容中直接将扩展作为一个对象来访问。

JUnit will call the extension methods at appropriate stages.

JUnit将在适当的阶段调用扩展方法。

For example, if an extension implements BeforeEachCallback, JUnit will call its corresponding interface methods before executing a test method.

例如,如果一个扩展实现了BeforeEachCallback, JUnit将在执行测试方法之前调用其相应的接口方法。

3. Using @RegisterExtension With Static Fields

3.使用@RegisterExtension与静态字段

When used with static fields, JUnit will apply the methods of this extension after the class-level @ExtendWith based extensions have been applied.

当与静态字段一起使用时,JUnit将在基于类级@ExtendWith的扩展被应用后应用该扩展的方法。

Also, JUnit will invoke both class-level and method-level callbacks of the extension.

同时,JUnit将调用扩展的类级和方法级的回调。

For example, the following extension features both a beforeAll and a beforeEach implementation:

例如,下面的扩展同时具有beforeAllbeforeEach的实现。

public class LoggingExtension implements 
  BeforeAllCallback, BeforeEachCallback {

    // logger, constructor etc

    @Override
    public void beforeAll(ExtensionContext extensionContext) 
      throws Exception {
        logger.info("Type {} In beforeAll : {}", 
          type, extensionContext.getDisplayName());
    }

    @Override
    public void beforeEach(ExtensionContext extensionContext) throws Exception {
        logger.info("Type {} In beforeEach : {}",
          type, extensionContext.getDisplayName());
    }

    public String getType() {
        return type;
    }
}

Let’s apply this extension to a static field of a test:

让我们把这个扩展应用于一个测试的静态字段。

public class RegisterExtensionTest {

    @RegisterExtension
    static LoggingExtension staticExtension = new LoggingExtension("static version");

    @Test
    public void demoTest() {
        // assertions
    }
}

The output shows messages from both the beforeAll and beforeEach methods:

输出显示来自beforeAllbeforeEach方法的信息。

Type static version In beforeAll : RegisterExtensionTest
Type static version In beforeEach : demoTest()

4. Using @RegisterExtension With Instance Fields

4.使用@RegisterExtension与实例字段

If we use RegisterExtension with non-static fields, JUnit will only apply the extension after processing all TestInstancePostProcessor callbacks.

如果我们使用带有非静态字段的RegisterExtension,JUnit将只在处理完所有TestInstancePostProcessor回调后应用扩展。

In this case, JUnit will ignore class level callbacks like beforeAll.

在这种情况下,JUnit将忽略像beforeAll这样的类级回调。

In the above example, let’s remove the static modifier from LoggingExtension:

在上面的例子中,让我们从LoggingExtension中移除static修改器。

@RegisterExtension
LoggingExtension instanceLevelExtension = new LoggingExtension("instance version");

Now JUnit will only invoke the beforeEach method, as seen in the output:

现在JUnit将只调用beforeEach方法,正如在输出中看到的那样。

Type instance version In beforeEach : demoTest()

5. Conclusion

5.结论

In this article, we did an overview of programmatically registering JUnit 5 extensions with @RegisterExtension.

在这篇文章中,我们对用@RegisterExtension以编程方式注册JUnit 5扩展做了概述。

We also covered the difference between applying the extension to static fields vs. instance fields.

我们还讨论了将扩展应用于静态字段与实例字段之间的区别。

As usual, code examples can be found at our Github repository.

像往常一样,可以在我们的Github资源库找到代码示例。