1

Before all tests, it's needed to insert data into database.
After all tests, it's needed to remove all data from database.

In TestNG it's possible to do such stuff using @BeforeSuite and @AfterSuite.
How to do such stuff in JUnit 5?

In Junit 5 @BeforeAll annotation marks a method to run before all tests in a class.
@AfterAll annotation marks a method to run after all tests in a class.

I found following solution on stackoverflow:
https://stackoverflow.com/a/51556718/6643395

But:

  • it works only for Before all tests, not After all tests
  • it is needed to annotate each test class with this extension. And that is not good. In my project all test classes extend some base test class. So, why not do this stuff in one place?
Artur
  • 661
  • 1
  • 6
  • 23

1 Answers1

1

Maybe it was added later, but the answer you referenced actually includes a hook to run code after all tests of all classes have finished, by using a CloseableResource. This is exactly what you're looking for.

And you can also annotate your super class with the extension, as @ExtendWith is inherited. If you use your custom meta-annotation, you'll have to annotate that as @Inherited, too. In this way, you can do the stuff in one place.

BTW: it's often even easier to not inherit from common super classes but to use extensions everywhere instead.

rü-
  • 2,129
  • 17
  • 37