I am trying to add a feature to a mocking framework to have a test class fail when there is a method that has been stubbed but never actually invoked, i.e. either the test contains too much stubbing or the tested code didn't follow the intended path.
I can already do the check, and I was planning on doing it in the @AfterAll method implemented by a JUnit extension, something like this:
@ExtendWith(MyExtension::class)
class TestClass {
@Test
fun myTest() {
// stub a function
// run some test code
}
}
class MyExtension : AfterAllCallback {
override fun afterAll(context: ExtensionContext) {
if (not all stubbed methods have been called) {
throw AssertionError("Something")
}
}
}
However, the AssertionError being thrown in the afterAll body does not make the tests fail.
It kind of makes sense to me, since afterAll is expected to be executed after all the tests, when the tests result has already been computed, but I was wondering if there was a way to have a test class failure triggered after all tests have been executed.