Home > OS >  Junit5 never hitting @BeforeAll inside @Suite class
Junit5 never hitting @BeforeAll inside @Suite class

Time:08-13

Currently going through the process of upgrading from Junit4 to Junit5 and running into a bit of a hurdle with something that used to work in Junit4.

Wondering if there is a way to access the @BeforeAll/@AfterAll (formerly @BeforeClass/@AfterClass) within a class which is also a @Suite

Example:

@Suite
@SelectClasses({
        SquareService.class,
        TriangleService.class
})
public class ShapeTestSuite { 

    @BeforeAll
    public void beforeAll() {
        Log.info("Shape Test Suite Start!");
    }

    @AfterAll
    public void afterAll() {
        Log.info("Shape Test Suite End!");
    }

}

I've tried adding an extension to this class/suite, but that also does not seem to work. What am I missing? Any help would be greatly appreciated.

Thanks

CodePudding user response:

@BeforeAll and @AfterAll are annotations from Jupiter, which is one (of many) JUnit 5 engines. @Suite, however, is independent and can run tests and test suites combined from any JUnit 5 engine. That’s why you cannot use a before-all method in a suite class.

CodePudding user response:

@BeforeAll and @AfterAll runs before any execution of @Test, @TestFactory, etc. So if you create one them you will see that your code will be run correctly.

  • Related