spring-framework
spring-framework copied to clipboard
`@MockitoBean` does not get reset in inherited `@Nested` tests
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 https://github.com/spring-projects/spring-boot/issues/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 {
}