-
Couldn't load subscription status.
- Fork 38.8k
Description
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:
jesperancinha/jeorg-spring-test-drives#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!