Skip to content

@MockitoBean does not work with verify like @MockBean did #33991

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

Closed
jesperancinha opened this issue Nov 29, 2024 · 5 comments
Closed

@MockitoBean does not work with verify like @MockBean did #33991

jesperancinha opened this issue Nov 29, 2024 · 5 comments
Assignees
Labels
in: test Issues in the test module status: duplicate A duplicate of another issue type: regression A bug that is also a regression

Comments

@jesperancinha
Copy link

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!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 29, 2024
@sbrannen sbrannen self-assigned this Nov 29, 2024
@sbrannen sbrannen added the in: test Issues in the test module label Nov 29, 2024
@sbrannen sbrannen changed the title @MockitoBean is not working with verify like @MockBean did @MockitoBean does not work with verify like @MockBean did Nov 30, 2024
@sbrannen
Copy link
Member

sbrannen commented Nov 30, 2024

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:

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

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2024
@sbrannen sbrannen added status: duplicate A duplicate of another issue 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 30, 2024
@jesperancinha
Copy link
Author

Thanks for your response @sbrannen . I will try this in a separate branch. When is the new release with the fix available?

@jesperancinha

This comment was marked as off-topic.

@sbrannen

This comment was marked as off-topic.

@sbrannen
Copy link
Member

sbrannen commented Dec 1, 2024

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.

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 status: duplicate A duplicate of another issue type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants