Merge branch '1.5.x'

This commit is contained in:
Andy Wilkinson
2018-02-05 10:54:49 +00:00
4 changed files with 208 additions and 15 deletions

View File

@@ -28,6 +28,7 @@ import org.mockito.MockitoAnnotations;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
@@ -35,6 +36,7 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Tests for {@link MockitoTestExecutionListener}.
@@ -78,6 +80,28 @@ public class MockitoTestExecutionListenerTests {
assertThat(this.fieldCaptor.getValue().getName()).isEqualTo("mockBean");
}
@Test
public void beforeTestMethodShouldDoNothingWhenDirtiesContextAttributeIsNotSet()
throws Exception {
WithMockBean instance = new WithMockBean();
this.listener.beforeTestMethod(mockTestContext(instance));
verifyNoMoreInteractions(this.postProcessor);
}
@Test
public void beforeTestMethodShouldInjectMockBeanWhenDirtiesContextAttributeIsSet()
throws Exception {
WithMockBean instance = new WithMockBean();
TestContext mockTestContext = mockTestContext(instance);
given(mockTestContext.getAttribute(
DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))
.willReturn(Boolean.TRUE);
this.listener.beforeTestMethod(mockTestContext);
verify(this.postProcessor).inject(this.fieldCaptor.capture(), eq(instance),
(MockDefinition) any());
assertThat(this.fieldCaptor.getValue().getName()).isEqualTo("mockBean");
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private TestContext mockTestContext(Object instance) {
TestContext testContext = mock(TestContext.class);