3

TL;DR Is there any way we could annotate a full class with an extension, while at the same time being able to intercept all the tests and invoke them multiple times?

We are trying to update one of our projects from Junit4 to Junit5 (find the link here) but we have come across some problems in the migration from using Rules, Runners and Statements to the equivalents using JUnit5 Extensions.

The project performs mutation testing on SQL statements where each test method is run multiple times, one for each possible mutation of the SQL in order to generate a code coverage report of the SQL. Currently we do this using a JUnit4 Rule but we want to change this to support JUnit 5, so will need to change this Rule to an Extension. The way this currently works in the Rule is that when a (@Test) method gets called it in turn calls the Rule's apply() method and we then use this to return a custom Statement object. In this Statement we create the mutants and then call the base evaluate() method multiple times, once for each mutant, and monitor and store the results.

We tried looking into how to do the equivalent in JUnit5 with an extension but the similar functionality provided by Repeated and Paramaterized (which both run methods multiple times) require adding an annotation to every single method they apply to. We would prefer to just use @ExtendWith and then be able to intercept all test method calls and inject our multiple call functionality into all of them. We looked into how @Repeated and @Paramterized do this via the TestTemplate annotations and the 'TestTemplateInvocationContextProvider', but adding the annotation at the method level is something we want to avoid as generally users want coverage reports based on the entire class, not single methods. We also looked at various JUnit5 lifecycle interfaces that an Extension can implement, like InvocationInterceptor, but this only allows us to call proceed() once on the method being invoked while we need to do this multiple times.

Is there any way we could annotate a full class with an extension, while at the same time being able to intercept the tests and make changes to them?

Thank you so much for your time!

shermosa
  • 31
  • 2

0 Answers0