Skip to content

@MockitoBean does not get reset in inherited @Nested tests #34906

Open
@stan-borissov

Description

@stan-borissov

In the snippet below, the ServiceB MockBean is not reset between firstTest and secondTest, but it should be. This causes secondTest to fail on verify(serviceB).bar();, as there are 2 invocations instead of just the expected 1.

Note that the @Import(ServiceC.class) on ChildTest is not required for the test as such, but causes (by some mysterious means) the failure to reset ServiceB, as removing the @Import will make the test pass.

Spring Boot version is 3.4.5.

This issue may be related to spring-projects/spring-boot#12470.

Services

@Component
public class ServiceA {
    @Autowired
    public ServiceB serviceB;

    public void foo() {
        serviceB.bar();
    }
}
@Component
public class ServiceB {
    public void bar() {
    }
}
@Component
public class ServiceC {
}

Test

@SpringBootTest
public abstract class ParentTest {

    @MockBean
    ServiceB serviceB;
    @Autowired
    ServiceA serviceA;

    @Nested
    class NestedTest {

        @Test
        void firstTest() {
            serviceA.foo();
            verify(serviceB).bar();
        }

        @Test
        void secondTest() {
            serviceA.foo();
            verify(serviceB).bar();
        }
    }
}
@Import(ServiceC.class)
public class ChildTest extends ParentTest {
}

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: waiting-for-triageAn issue we've not yet triaged or decided on

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions