Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

1. Overview

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

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

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.

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

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

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).

Let's redo our test class and add the @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

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.

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

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Junit footer banner
Comments are closed on this article!