-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
MockitoTestExecutionListener
no longer opens mocks in the prepareTestInstance()
callback
#33690
Comments
@Mock
fields are no longer initialized before a @BeforeAll
method is call resulting in an NPE@Mock
fields are no longer initialized before a @BeforeAll
method is called resulting in an NPE
Thanks for reporting the issue, @wilkinsona. 👍🏻
I confirm that this is a change in behavior in Spring Framework since 6.2 RC1, and it is in fact caused by the removal of the However, that change was made in order to provide better support for If we reinstate the On the other hand, if we reintroduce the In retrospect, neither of those scenarios is desirable, and I'll address that topic in a new GitHub issue. In addition, I confirmed that Spring Boot never supported the use cases you have provided, and Mockito's own For example, the following all fail due to a @SpringBootTest
@TestExecutionListeners({
org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.class,
org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.class
})
class MockInitInBeforeAllWithSpringBootTests {
@Mock
static List<String> mock;
@BeforeAll
static void setUp() {
given(mock.size()).willReturn(1);
}
@Test
void shouldSetUpSuccessfully() {
assertThat(mock.size()).isEqualTo(1);
}
} @SpringBootTest
@TestExecutionListeners({
org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener.class,
org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.class
})
class MockInitInBeforeEachWithSpringBootTests {
@Mock
List<String> mock;
@BeforeEach
void setUp() {
given(mock.size()).willReturn(1);
}
@Test
void shouldSetUpSuccessfully() {
assertThat(mock.size()).isEqualTo(1);
}
} @ExtendWith(MockitoExtension.class)
class MockInitInBeforeAllWithMockitoExtensionTests {
@Mock
private static List<String> mock;
@BeforeAll
static void setUp() {
given(mock.size()).willReturn(1);
}
@Test
void shouldSetUpSuccessfully() {
assertThat(mock.size()).isEqualTo(1);
}
} However, the @ExtendWith(MockitoExtension.class)
class MockitoInitInBeforeEachWithMockitoExtensionTests {
@Mock
private List<String> mock;
@BeforeEach
void setUp() {
given(mock.size()).willReturn(1);
}
@Test
void shouldSetUpSuccessfully() {
assertThat(mock.size()).isEqualTo(1);
}
} @ExtendWith(SpringExtension.class)
class MockitoInitInBeforeEachWithSpringExtensionTests {
@Mock
private List<String> mock;
@BeforeEach
void setUp() {
given(mock.size()).willReturn(1);
}
@Test
void shouldSetUpSuccessfully() {
assertThat(mock.size()).isEqualTo(1);
}
} In summary, if we wish to continue using the In light of that, I am closing this issue. |
I’m surprised that this has been closed, particularly with the assertion that “Spring Boot never supported the use cases you have provided” when, as I mentioned above, there is a test in Spring Boot that started failing with 6.2.0-SNAPSHOT. It succeeds with 6.1.x (Boot 3.3) and with 6.2.0-RC1. You can find the test class here. It’s |
Hi Andy, Thanks for the feedback.
I misunderstood your original claim. I wrongly assumed you meant the regression was for behavior introduced in Spring Framework that did not exist previously in Spring Boot, but now I see that the My apologies! I'm reopening this issue to make sure we take that into consideration. |
@Mock
fields are no longer initialized before a @BeforeAll
method is called resulting in an NPEMockitoTestExecutionListener
no longer opens mocks in the prepareTestInstance()
callback
The Spring Cloud team is running into an issue with this change as well (I think). This test has started failing because the variables annotated with
|
We've come full circle here and decided to reinstate support for the init of Mockito's mocks in Spring Boot. I think this can be closed in favor of spring-projects/spring-boot#42708. The goals of reinstating the support in Boot are two-fold:
Hopefully this buys us some time to provide a better solution in Boot 4.0/Framework 7.0 while not making things rough for Boot users when they upgrade to 3.4. |
Thanks @wilkinsona! The changes in Boot seem to be working for us! |
Thanks for the feedback, @wilkinsona and @ryanjbaxter. In light of that, I am closing this as superseded by: |
Affects: 6.2.0-SNAPSHOT
This is a recent regression from 6.2.0-RC1. I believe it was introduced in eb4bf1c#diff-02b9cc5ba2d6121e1a5f63ad339bb82fa8f3531d568c43d07fac61ade962c0cd where the override of
prepareTestInstance
was removed. The result is that trying to call a@Mock
-annotated field in a@BeforeAll
method fails with an NPE.This should reproduce the regression:
Another, more advanced variant that uses a custom test instance lifecycle and which is closer to the Spring Boot test that found the regression also fails with 6.2.0-SNAPSHOT:
The text was updated successfully, but these errors were encountered: