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

Bean overriding in tests should use by-type semantic if no name is specified #32761

Closed
hantsy opened this issue May 4, 2024 · 7 comments
Closed
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@hantsy
Copy link
Contributor

hantsy commented May 4, 2024

Environment: Spring 6.2.0-M1, Java 21

Example project: https://github.com/hantsy/spring6-sandbox/tree/master/test-bean

I tried to write a test to experience the new @MockitoSpyBean.

@SpringJUnitConfig(classes = Config.class)
class CustomerServiceMockitoSpyTest {

    @MockitoSpyBean
    CustomerService customerService;

    @Test
    public void testCustomerService() {
        when(customerService.findByEmail("dummy@example.com"))
                .thenReturn(
                        new Customer("dummy first", "dummy last", "dummy@example.com")
                );

        // test bean
        var testCustomer = customerService.findByEmail("dummy@example.com");
        assertThat(testCustomer.firstName()).isEqualTo("dummy first");
        assertThat(testCustomer.lastName()).isEqualTo("dummy last");
        assertThat(customerService.findAll().size()).isEqualTo(2);

        verify(customerService, times(1)).findByEmail(anyString());
        verify(customerService, times(1)).findAll();
        verifyNoMoreInteractions(customerService);
    }
}

I tried to use customerServiceSpy as the field name here, but it does not work. I have to use customerSerivce here and set the bean name attribute.

Whereas, in the test that's testing @MockitoBean in CustomerServiceMockitoTest, using customerServiceMock(no need set the bean name) worked well.

The spied field name should be named by developers freely, and the spied bean should override the real bean by type (not name) firstly.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label May 4, 2024
@sbrannen sbrannen added the in: test Issues in the test module label May 4, 2024
@sbrannen sbrannen changed the title @MickitoSpyBean limted to use bean name @MockitoSpyBean is required to use bean name May 4, 2024
@snicoll snicoll changed the title @MockitoSpyBean is required to use bean name Bean overriding in tests should use by-type semantic if no name is specified May 5, 2024
@snicoll
Copy link
Member

snicoll commented May 5, 2024

See also #32760.

It looks like bean overriding does not work with a by-type semantic. While that's a choice we can make, it looks like the Mockito support in Spring Boot does do that. If I haven't overlooked anything, this seems to me a too intrusive change of behavior for people moving from the Spring Boot support, so we should support that as well IMO.

@hantsy

This comment was marked as resolved.

@aditya78910

This comment was marked as resolved.

@snicoll

This comment was marked as resolved.

@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 8, 2024
@snicoll snicoll added this to the 6.2.x milestone May 8, 2024
@simonbasle
Copy link
Contributor

Note that in the second case (CustomerServiceMockitoTest) it appears to work because a new bean named customerServiceMock is created (per bean overriding CREATE_OR_REPLACE strategy). So the context now contains two beans of type CustomerService: the prod one and the mock one.

I've started making modifications to override based on type by default, but I need to clarify what should happen in the case where multiple candidates are found when looking up that way.

@snicoll
Copy link
Member

snicoll commented May 13, 2024

I need to clarify what should happen in the case where multiple candidates are found when looking up that way.

I am wondering why we wouldn’t consider what Spring Boot does for a start. The algorithm looks straightforward enough to me, with the addition of the qualifier in the metadata.

@simonbasle
Copy link
Contributor

@TestBean, @MockitoBean and @MockitoSpyBean now match a bean by type if no explicit bean name is provided. I would nonetheless encourage users to be as explicit as possible (i.e. specify a bean name) when overriding beans.

I will follow up in another issue for M3 (#32822) to introduce the option of also using qualifiers.

@simonbasle simonbasle modified the milestones: 6.2.x, 6.2.0-M2 May 14, 2024
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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

6 participants