Hello there!
I am having a problem where the soon to be removed @MockBean
annotation is working but the new one @MockitoBean
isn't. On my modules and projects where I use MockK, this isn't of course a problem, but I have many projects that also use mockito. Here is a code example:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
@ExtendWith(MockitoExtension.class)
class SpringFlash32LauncherTemplateTest {
@MockBean()
// @MockitoBean This doesn't work
private JdbcTemplate jdbcTemplate;
@Captor
private ArgumentCaptor<String> stringArgumentCaptor;
@Test
void testContext() {
verify(jdbcTemplate, times(1)).execute("CREATE TABLE WHEN_MUSIC(\n" +
" ID INT NOT NULL AUTO_INCREMENT,\n" +
" ARTIST VARCHAR(255) NOT NULL,\n" +
" WHEN_MUSIC VARCHAR(255) NOT NULL,\n" +
" PRIMARY KEY (ID)\n" +
");");
verify(jdbcTemplate, times(1)).update("INSERT INTO WHEN_MUSIC(ARTIST, WHEN_MUSIC) VALUES (?, ?)", "The Doors", "The Music's Over");
verify(jdbcTemplate, times(1)).update("INSERT INTO WHEN_MUSIC(ARTIST, WHEN_MUSIC) VALUES (?, ?)", "Green Day", "I come around");
verify(jdbcTemplate, times(1)).query(stringArgumentCaptor.capture(), any(RowCallbackHandler.class));
assertThat(stringArgumentCaptor.getValue()).isEqualTo("SELECT * FROM WHEN_MUSIC");
}
}
I'm trying to get this pull request to work after quite a few tries, but to no avail yet:
https://github.com/jesperancinha/jeorg-spring-test-drives/pull/736
The original class can be found here: https://github.com/jesperancinha/jeorg-spring-test-drives/blob/master/jeorg-spring/jeorg-spring-flash/jeorg-spring-flash-set-3/jeorg-spring-flash-3-2/src/test/java/org/jesperancinha/std/flash32/rowcallbackhandler/SpringFlash32LauncherTemplateTest.java
When I run the test, it seems like the verify never works and I get something like this:
2024-11-29T17:04:10.090+01:00 INFO 73416 --- [ main] .s.f.r.SpringFlash32LauncherTemplateTest : Started SpringFlash32LauncherTemplateTest in 3.802 seconds (process running for 5.304)
Wanted but not invoked:
jdbcTemplate.execute(
"CREATE TABLE WHEN_MUSIC(
ID INT NOT NULL AUTO_INCREMENT,
ARTIST VARCHAR(255) NOT NULL,
WHEN_MUSIC VARCHAR(255) NOT NULL,
PRIMARY KEY (ID)
);"
);
-> at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:426)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
jdbcTemplate.execute(
"CREATE TABLE WHEN_MUSIC(
ID INT NOT NULL AUTO_INCREMENT,
ARTIST VARCHAR(255) NOT NULL,
WHEN_MUSIC VARCHAR(255) NOT NULL,
PRIMARY KEY (ID)
);"
);
-> at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:426)
Actually, there were zero interactions with this mock.
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:426)
at org.jesperancinha.std.flash32.rowcallbackhandler.SpringFlash32LauncherTemplateTest.testContext(SpringFlash32LauncherTemplateTest.java:32)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Let me know what to do. I can't move further with this project at the moment. Cheers!
Comment From: sbrannen
Hi @jesperancinha,
Congratulations on submitting your first issue for the Spring Framework! 👍
The interaction with the JdbcTemplate
mock in your CommandLineRunner
occurs during the refresh phase of the ApplicationContext
, and due to a bug in the MockReset
support that mock gets reset before your testContext()
test method.
Thus, this is effectively a:
- Duplicate of #33941
And I apologize for the inconvenience.
In any case, if you get a chance please try out 6.2.1-SNAPSHOT builds and let us know if your issue is resolved.
Cheers,
Sam
Comment From: jesperancinha
Thanks for your response @sbrannen . I will try this in a separate branch. When is the new release with the fix available?
Comment From: jesperancinha
@sbrannen , I'm afraid this isn't fixed yet. I made 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, please refrain from cross-posting this here as well.
It's sufficient to provide feedback on one issue.
Comment From: sbrannen
When is the new release with the fix available?
As can be seen in #33941, the fix will be included in Spring Framework 6.2.1.