@BeforeAll and @AfterAll in Non-Static Methods – 非静态方法中的@BeforeAll和@AfterAll

最后修改: 2020年 9月 28日

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

1. Overview

1.概述

In this short tutorial, we’re going to implement non-static methods with @BeforeAll and @AfterAll annotations available in Junit5.

在这个简短的教程中,我们将用@BeforeAll@AfterAll注解实现非静态方法,这些注解在Junit5中可用。

2. @BeforeAll and @AfterAll in Non-Static Methods

2.非静态方法中的@BeforeAll@AfterAll

While unit testing, we may occasionally want to use @BeforeAll and @AfterAll in non-static setup and tear-down methods — for instance, in a @Nested test class or as interface default methods.

在单元测试时,我们可能偶尔想在非静态的设置和拆除方法中使用@BeforeAll@AfterAll–例如,在@Nested测试类中或作为接口默认方法。

Let’s create a test class with the @BeforeAll and @AfterAll methods as non-static:

让我们创建一个测试类,将@BeforeAll@AfterAll方法定为非静态。

public class BeforeAndAfterAnnotationsUnitTest {

    String input;
    Long result;

    @BeforeAll
    public void setup() {
        input = "77";
    }

    @AfterAll
    public void teardown() {
        input = null;
        result = null;
    }

    @Test
    public void whenConvertStringToLong_thenResultShouldBeLong() {
        result = Long.valueOf(input);
        Assertions.assertEquals(77l, result);
    }​
}

If we run the above code, it will throw an exception:

如果我们运行上述代码,它将抛出一个异常。

org.junit.platform.commons.JUnitException:  ...

Let’s now see how we can avoid this situation.

现在让我们看看如何避免这种情况。

3. The @TestInstance Annotation

3.@TestInstance注释

We’ll use the @TestInstance annotation to configure the lifecycle of a test. If we don’t declare it on our test class, the lifecycle mode will be PER_METHOD by default. So, to prevent our test class from throwing a JUnitException, we need to annotate it with @TestInstance(TestInstance.Lifecycle.PER_CLASS).

我们将使用@TestInstance注解来配置测试的生命周期。如果我们不在我们的测试类上声明它,生命周期模式将默认为PER_METHOD。所以,为了防止我们的测试类抛出JUnitException,我们需要用@TestInstance(TestInstance.Lifecycle.PER_CLASS).注释它。

Let’s redo our test class and add the @TestInstance(TestInstance.Lifecycle.PER_CLASS):

让我们重做我们的测试类,并添加@TestInstance(TestInstance.Lifecycle.PER_CLASS):

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class BeforeAndAfterAnnotationsUnitTest {

    String input;
    Long result;

    @BeforeAll
    public void setup() {
        input = "77";
    }

    @AfterAll
    public void teardown() {
        input = null;
        result = null;
    }

    @Test
    public void whenConvertStringToLong_thenResultShouldBeLong() {
        result = Long.valueOf(input);
        Assertions.assertEquals(77l, result);
    }
}

In this case, our test runs successfully.

在这种情况下,我们的测试成功运行。

4. Conclusion

4.总结

In this short article, we’ve learned how to use @BeforeAll and @AfterAll in non-static methods. First, we started with a simple non-static example to show what happens if we don’t include the @TestInstance annotation. Then, we annotated our test with @TestInstance(TestInstance.Lifecycle.PER_CLASS) to prevent throwing a JUnitException.

在这篇短文中,我们学习了如何在非静态方法中使用@BeforeAll@AfterAll。首先,我们从一个简单的非静态例子开始,展示了如果我们不包含@TestInstance注解会发生什么。然后,我们用@TestInstance(TestInstance.Lifecycle.PER_CLASS)注释了我们的测试,以防止抛出JUnitException

As always, the implementation of all these examples is over on GitHub.

一如既往,所有这些例子的实现都在GitHub上