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

MockReset strategy is no longer honored for @MockitoBean and @MockitoSpyBean #33941

Closed
quaff opened this issue Nov 22, 2024 · 8 comments
Closed
Assignees
Labels
in: test Issues in the test module type: regression A bug that is also a regression
Milestone

Comments

@quaff
Copy link
Contributor

quaff commented Nov 22, 2024

@SpringJUnitConfig
class MockBeanTests {

	@MockitoBean
	// @MockBean works
	ContextRefreshedEventProcessor contextRefreshedEventProcessor;

	@Test
	void test() {
		then(contextRefreshedEventProcessor).should().process(any(ContextRefreshedEvent.class));
	}

	@Configuration
	static class Config {

		@Bean
		ContextRefreshedEventListener contextRefreshedEventListener(
				ContextRefreshedEventProcessor contextRefreshedEventProcessor) {
			return new ContextRefreshedEventListener(contextRefreshedEventProcessor);
		}

	}

	static class ContextRefreshedEventListener {

		private final ContextRefreshedEventProcessor contextRefreshedEventProcessor;

		public ContextRefreshedEventListener(ContextRefreshedEventProcessor contextRefreshedEventProcessor) {
			this.contextRefreshedEventProcessor = contextRefreshedEventProcessor;
		}

		@EventListener
		public void onApplicationEvent(ContextRefreshedEvent event) {
			contextRefreshedEventProcessor.process(event);
		}

	}

	interface ContextRefreshedEventProcessor {
		void process(ContextRefreshedEvent event);
	}
}

The test fails with @MockitoBean but works fine with Spring Boot's @MockBean.

I think it's caused by @MockitoBean resetting the mock before tests automatically.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 22, 2024
@sbrannen sbrannen added the in: test Issues in the test module label Nov 23, 2024
@sbrannen sbrannen self-assigned this Nov 23, 2024
@sbrannen sbrannen added type: regression A bug that is also a regression and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 24, 2024
@sbrannen sbrannen added this to the 6.2.1 milestone Nov 24, 2024
@sbrannen sbrannen changed the title Provide a way to not reset @MockitoBean before performing tests @MockitoBean is always reset before tests, ignoring MockReset.AFTER strategy Nov 24, 2024
@sbrannen
Copy link
Member

sbrannen commented Nov 24, 2024

Thanks for bringing this to our attention, @quaff! 👍

I apologize: I inadvertently removed the MockReset check for @MockitoBean and MockitoSpyBean while refactoring MockitoResetTestExecutionListener in 6c2cba5.

So, I'll add that back and make sure we have tests in place that cover this use case.

@sbrannen sbrannen changed the title @MockitoBean is always reset before tests, ignoring MockReset.AFTER strategy MockReset strategy is no longer honored for @MockitoBean and @MockitoSpyBean Nov 25, 2024
@quaff
Copy link
Contributor Author

quaff commented Nov 27, 2024

I confirm it's fixed now, thank you @sbrannen

@sbrannen
Copy link
Member

I confirm it's fixed now,

Great! Thanks for letting us know, @quaff. 👍

thank you @sbrannen

You're welcome.

sbrannen added a commit that referenced this issue Nov 29, 2024
As a follow up to commit 0088b9c, this commit introduces an
integration test which verifies that a spy created via @⁠MockitoSpyBean
using the MockReset.AFTER strategy is not reset between the refresh of
the ApplicationContext and the first use of the spy within a @⁠Test
method.

See gh-33941
See gh-33986
@jesperancinha
Copy link

@sbrannen , I'm afraid this isn't fixed yet. I made a pull request with your suggestion and it just doesn't build and it throws the same error: jesperancinha/jeorg-spring-test-drives#737

@sbrannen
Copy link
Member

sbrannen commented Dec 1, 2024

@jesperancinha, you only updated the dependency for spring-context; whereas, the modified code is in spring-test.

In other words, you are still running your test against spring-test 6.2.0.

Thus, you will at the very least need to update the dependency for spring-test to 6.2.1-SNAPSHOT. However, if you are using Spring Boot's dependency management you should upgrade the Spring Framework version appropriately (as documented here for Maven).

@jesperancinha
Copy link

Thanks for your response @sbrannen ! You did only mention spring-context and I just wanted to make sure that your next release would work. I see that it does and so I'm hopeful this won't be a problem on the next release. That's how much I care about the spring framework. Anyway, thank you for your input and for getting this issue fixed. I'll try to remember to remove the snapshot repo on the next release, but I'll probably forget it, and its not that important for one single module.

@sbrannen
Copy link
Member

sbrannen commented Dec 2, 2024

Hi @jesperancinha,

Thanks for your response @sbrannen!

You're welcome.

You did only mention spring-context

I actually did not mention any specific artifact.

Though I see that the Spring Framework Artifacts wiki page consistently uses spring-context as an example, and perhaps that is what mislead you.

So, I'll update that wiki page to provide greater clarity.

I just wanted to make sure that your next release would work. I see that it does and so I'm hopeful this won't be a problem on the next release.

Thanks for letting us know that your issue is resolved with the snapshots.

I'll try to remember to remove the snapshot repo on the next release, but I'll probably forget it, and its not that important for one single module.

Snapshots are only meant for trying out a fix locally before the official next release. More importantly, it is not recommended to use snapshot dependencies in production. So, you should definitely set a reminder or create an issue in your issue tracker to make sure you stop using snapshot dependencies.

Regards,

Sam

@sbrannen
Copy link
Member

sbrannen commented Dec 2, 2024

Though I see that the Spring Framework Artifacts wiki page consistently uses spring-context as an example, and perhaps that is what mislead you.

So, I'll update that wiki page to provide greater clarity.

FYI: I addressed that in #34002.

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: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

4 participants