Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding @MockitoSettings to field with @RegisterExtension makes test fail with PreconditionViolationException #3254

Open
magicwerk opened this issue Jan 29, 2024 · 0 comments

Comments

@magicwerk
Copy link

My test uses Mockito mocks so I want to use MockitoExtension supporting this.
I also need another extension SetupExtension which I must be able to configure in the test method, so this extension is added using @RegisterExtension.
I need to control the order of initialization, so I use @order and therefore also declare MockitoExtension using @RegisterExtension.
I need to use Strictness.LENIENT on the MockitoExtension.
However if I add @MockitoSettings to the mockitoExtension field, test fails.
I did not find any other way to set strictness, as constructor MockitoExtension(Strictness strictness) is private.

See the following test case reproducing the issue.
If test is executed, it fails with

org.junit.platform.commons.PreconditionViolationException: Failed to register extension via field [org.mockito.junit.jupiter.MockitoExtension eval.mockito.EvalMockitoJUnitExtension.mockitoExtension]. The field registers an extension of type [org.mockito.junit.jupiter.MockitoExtension] via @RegisterExtension and @ExtendWith, but only one registration of a given extension type is permitted.

If I comment the line
// @MockitoSettings(strictness = Strictness.LENIENT)
the test runs fine (but I need to be able to set the strictness)

Versions used:
'org.mockito:mockito-core:5.10.0'
'org.mockito:mockito-junit-jupiter:5.10.0'
'org.junit.jupiter:junit-jupiter:5.10.1'
'org.junit.jupiter:junit-jupiter-engine:5.10.1'
'org.junit.platform:junit-platform-launcher:1.10.0'

public class EvalMockitoJUnitExtension {

	static class SetupExtension implements BeforeEachCallback, AfterEachCallback {
		static String message;

		@Override
		public void beforeEach(ExtensionContext context) throws Exception {
		}

		@Override
		public void afterEach(ExtensionContext context) throws Exception {
		}

		public void setMessage(String message) {
			SetupExtension.message = message;
		}

		public static String getMessage() {
			return message;
		}
	}

	static class SetupClient {
		public String getMessage() {
			return SetupExtension.getMessage();
		}
	}

	@RegisterExtension
	@Order(0)
	SetupExtension setupExtension = new SetupExtension();

	@RegisterExtension
	@Order(1)
	@MockitoSettings(strictness = Strictness.LENIENT)
	MockitoExtension mockitoExtension = new MockitoExtension();

	@Spy
	SetupClient setupClient;

	@Test
	public void test() {
		setupExtension.setMessage("active");
		System.out.print("Message: " + setupClient.getMessage());
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant