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

Resetting of a @MockitoBean does not work when combined with @Nested #33676

Open
wilkinsona opened this issue Oct 9, 2024 · 0 comments
Open
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
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