Description
openedon Aug 27, 2023
Overview
As discovered by @graydavid in #3433, there is an issue regarding extension registration via @ExtendWith
on instance fields.
The following is a simplification of the RandomNumberDemo
from the User Guide which demonstrates the problem.
// @ExtendWith(RandomNumberExtension.class)
class RandomNumberDemoTest {
@Random
Integer randomNumber;
@Test
void test() {
assertThat(this.randomNumber).isNotNull();
}
}
The above test fails; however, if you annotate the test class with @ExtendWith(RandomNumberExtension.class)
the test then passes.
The reason is that ClassBasedTestDescriptor#instantiateAndPostProcessTestInstance()
invokes ExtensionUtils.registerExtensionsFromFields()
too late.
I even added the following comment when I introduced that support.
// In addition, we register extensions from instance fields here since the
// best time to do that is immediately following test class instantiation
// and post processing.
In retrospect, that is not the "best time to do that" when a field is annotated (or meta-annotated in the case of @Random
) with an @ExtendWith
annotation that registers an extension that implements TestInstancePostProcessor
.
Related Issues
- Fix implementation of
RandomNumberExtension
in the User Guide #3433 - Revise implementation of
RandomNumberExtension
in the User Guide #3463
Deliverables
- Revise the usage of
ExtensionUtils.registerExtensionsFromFields()
so that aTestInstancePostProcessor
can be registered via@ExtendWith
on a non-static field.
Activity