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

@MockitoBean reset and MockitoSession management do not work with @Nested tests #33676

Closed
wilkinsona opened this issue Oct 9, 2024 · 1 comment
Assignees
Labels
in: test Issues in the test module type: bug A general bug
Milestone

Comments

@wilkinsona
Copy link
Member

Affects: 6.2.0-SNAPSHOT

The following reproduces the problem:

package com.example;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.times;

@ExtendWith(SpringExtension.class)
class InheritedNestedTestConfigurationTests {

	@MockitoBean(enforceOverride = false)
	Action action;

	@Autowired
	ActionPerformer performer;

	@Test
	void mockWasInvokedOnce() {
		this.performer.run();
		then(this.action).should().perform();
	}

	@Test
	void mockWasInvokedTwice() {
		this.performer.run();
		this.performer.run();
		then(this.action).should(times(2)).perform();
	}

	@Nested
	class InnerTests {

		@Test
		void mockWasInvokedOnce() {
			InheritedNestedTestConfigurationTests.this.performer.run();
			then(InheritedNestedTestConfigurationTests.this.action).should().perform();
		}

		@Test
		void mockWasInvokedTwice() {
			InheritedNestedTestConfigurationTests.this.performer.run();
			InheritedNestedTestConfigurationTests.this.performer.run();
			then(InheritedNestedTestConfigurationTests.this.action).should(times(2)).perform();
		}

	}

	public interface Action {

		void perform();

	}

	static class ActionPerformer {

		private final Action action;

		ActionPerformer(Action action) {
			this.action = action;
		}

		void run() {
			this.action.perform();
		}

	}

	@Configuration(proxyBeanMethods = false)
	static class TestConfiguration {

		@Bean
		ActionPerformer actionPerformer(Action action) {
			return new ActionPerformer(action);
		}

	}

}

One of the two tests in InnerTests will fail because the mock will have been called more times than expected as it has not been reset between the two tests. I think this part of the problem was introduced in 65d2191. With a local fix for that in place, the failure still occurs. Looking in the debugger, this is because there are two instances of the action mock, one that has been injected into the tests and one that's being reset by MockitoResetTestExecutionListener.

@sbrannen sbrannen self-assigned this Oct 9, 2024
@sbrannen sbrannen added in: test Issues in the test module type: bug A general bug labels Oct 9, 2024
@sbrannen sbrannen added this to the 6.2.0-RC2 milestone Oct 9, 2024
@sbrannen sbrannen changed the title Resetting of a @MockitoBean does not work when combined with @Nested @MockitoBean reset and MockitoSession management do not work with @Nested tests Oct 11, 2024
@sbrannen
Copy link
Member

Thanks for the report and the reproducer! 👍

This has been fixed on main and will be available in upcoming 6.2 snapshots.

Please let us know if that addresses your issues.

With a local fix for that in place, the failure still occurs. Looking in the debugger, this is because there are two instances of the action mock, one that has been injected into the tests and one that's being reset by MockitoResetTestExecutionListener.

That appears to be a different bug in the Spring TestContext Framework.

If you annotate InheritedNestedTestConfigurationTests with @ContextConfiguration it should now run against 6.2 snapshots.

I will address that @ContextConfiguration (default @Configuration class detection) issue separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants