Search code examples
javaunit-testingjunitjunit4

Hook run before first test in any class in JUnit is executed


My project has dozen of JUnit 4 test classes with test cases.

Before any of them are run, I want to execute code (in my case to fix the logging in certain scenarios). How can I achieve that?

My test classes are too many to be reasonably enumerated in a test suite and @BeforeClass is not helpful here either.


Solution

  • There does not seem to exist an easy solution, so I ended up with a regular Spring bean initializied in the test Spring context to do the magic.