0

I would like to create a JUnit 5 test suite that runs some tests. However, I would like the test suite to execute something before it starts running the tests. Is there a way to do this? I have tried using the @BeforeAll annotation, but I don't think it's applicable in this sense, nothing seems to be happening.

I have:

@RunWith(JUnitPlatform.class)
@SelectPackages("org.foo")
public class FooITSuite
{

    @BeforeAll
    public void setUp()
    {
        System.out.println("Lift-off!!!");
    }

}

Is this at all possible?

The tests do execute, but the setUp method does not get executed.

carlspring
  • 31,231
  • 29
  • 115
  • 197
  • 1
    Using `@BeforeAll` is correct. Please edit your question to include a MCVE, which shows the problem you have. – Progman Dec 21 '20 at 18:08
  • Please add some more details. – Zahid Khan Dec 21 '20 at 18:13
  • @Progman: I've added a basic example. – carlspring Dec 21 '20 at 18:14
  • 3
    @carlspring The method must be `static`, see https://junit.org/junit5/docs/5.0.0/api/org/junit/jupiter/api/BeforeAll.html – Progman Dec 21 '20 at 18:17
  • @Progman: Thanks for pointing that out, but even if it's `static` it doesn't get executed. – carlspring Dec 21 '20 at 18:22
  • @carlspring Why do you use `@RunWith(JUnitPlatform.class)`? Also, how do you run the unit tests? – Progman Dec 21 '20 at 18:33
  • @Progman: Because that's what all the examples of how to use suites in JUnit 5 show. Is there a better way? I'm running the tests in both Idea and a terminal using Maven via the `maven-failsafe-plugin` as mentioned above. – carlspring Dec 21 '20 at 18:36
  • @Progman: Thanks for your help! This does indeed solve my problem! I also have another question that perhaps you might be able to help with: https://stackoverflow.com/questions/65398013/how-to-get-junit-tests-annotated-with-tag-to-be-executed-in-suite-that-has-inc ? – carlspring Dec 21 '20 at 20:22

0 Answers0