Skip to content

TestInstancePostProcessor cannot be registered via @ExtendWith on a non-static field #3437

Closed

Description

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

Deliverables

  • Revise the usage of ExtensionUtils.registerExtensionsFromFields() so that a TestInstancePostProcessor can be registered via @ExtendWith on a non-static field.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions