@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.
Comment From: sbrannen
Thanks for bringing this to our attention, @quaff! 👍
I apologize: I inadvertently removed the MockReset
check for @MockitoBean
and MockitoSpyBean
while refactoring MockitoResetTestExecutionListener
in 6c2cba5d8ad29657a5164dd1ad369064f39660cf.
So, I'll add that back and make sure we have tests in place that cover this use case.
Comment From: quaff
I confirm it's fixed now, thank you @sbrannen
Comment From: sbrannen
I confirm it's fixed now,
Great! Thanks for letting us know, @quaff. 👍
thank you @sbrannen
You're welcome.
Comment From: jesperancinha
@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: https://github.com/jesperancinha/jeorg-spring-test-drives/pull/737
Comment From: sbrannen
@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).
Comment From: jesperancinha
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.
Comment From: sbrannen
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
Comment From: sbrannen
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.