Ensure that remaining mocks are closed before initializing a test

Closes gh-39271
This commit is contained in:
Moritz Halbritter
2024-01-25 10:57:45 +01:00
parent 930669888a
commit df755db1f3
2 changed files with 127 additions and 1 deletions

View File

@@ -64,9 +64,10 @@ public class MockitoTestExecutionListener extends AbstractTestExecutionListener
}
@Override
public void beforeTestMethod(TestContext testContext) {
public void beforeTestMethod(TestContext testContext) throws Exception {
if (Boolean.TRUE.equals(
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
closeMocks(testContext);
initMocks(testContext);
reinjectFields(testContext);
}
@@ -77,6 +78,11 @@ public class MockitoTestExecutionListener extends AbstractTestExecutionListener
closeMocks(testContext);
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {
closeMocks(testContext);
}
private void initMocks(TestContext testContext) {
if (hasMockitoAnnotations(testContext)) {
testContext.setAttribute(MOCKS_ATTRIBUTE_NAME, MockitoAnnotations.openMocks(testContext.getTestInstance()));