Polish tests for TestContext lifecycle events
See gh-18490
This commit is contained in:
parent
a16dd95ab7
commit
7d926a847d
|
|
@ -35,7 +35,6 @@ import org.springframework.test.context.event.annotation.BeforeTestClass;
|
||||||
import org.springframework.test.context.event.annotation.BeforeTestExecution;
|
import org.springframework.test.context.event.annotation.BeforeTestExecution;
|
||||||
import org.springframework.test.context.event.annotation.BeforeTestMethod;
|
import org.springframework.test.context.event.annotation.BeforeTestMethod;
|
||||||
import org.springframework.test.context.event.annotation.PrepareTestInstance;
|
import org.springframework.test.context.event.annotation.PrepareTestInstance;
|
||||||
import org.springframework.util.ReflectionUtils;
|
|
||||||
|
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.only;
|
import static org.mockito.Mockito.only;
|
||||||
|
|
@ -47,27 +46,23 @@ import static org.mockito.Mockito.verify;
|
||||||
* accompanying {@link TestContextEvent} annotations.
|
* accompanying {@link TestContextEvent} annotations.
|
||||||
*
|
*
|
||||||
* @author Frank Scheffler
|
* @author Frank Scheffler
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 5.2
|
* @since 5.2
|
||||||
*/
|
*/
|
||||||
public class EventPublishingTestExecutionListenerIntegrationTests {
|
public class EventPublishingTestExecutionListenerIntegrationTests {
|
||||||
|
|
||||||
private TestContextManager testContextManager;
|
private final TestContextManager testContextManager = new TestContextManager(TestCase.class);
|
||||||
private TestContext testContext;
|
private final TestContext testContext = testContextManager.getTestContext();
|
||||||
private TestExecutionListener listener;
|
private final TestExecutionListener listener = testContext.getApplicationContext().getBean(EventCaptureConfiguration.class).listener();
|
||||||
private Object testInstance;
|
private final Object testInstance = new TestCase();
|
||||||
private Method testMethod;
|
private final Method testMethod = null;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initialize() {
|
public void resetMock() {
|
||||||
TestContextExposingTestContextManager tcm = new TestContextExposingTestContextManager();
|
// The mocked listener is a bean in the ApplicationContext that is stored
|
||||||
testContextManager = tcm;
|
// in a static cache by the Spring TestContext Framework.
|
||||||
testContext = tcm.getProtectedTestContext();
|
|
||||||
listener = testContext.getApplicationContext().getBean(EventCaptureConfiguration.class).trigger();
|
|
||||||
// reset because mock is a cached context bean
|
|
||||||
reset(listener);
|
reset(listener);
|
||||||
testInstance = new EmptyTestCase();
|
|
||||||
testMethod = ReflectionUtils.findMethod(EmptyTestCase.class, "dummyMethod");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -117,50 +112,50 @@ public class EventPublishingTestExecutionListenerIntegrationTests {
|
||||||
static class EventCaptureConfiguration {
|
static class EventCaptureConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public TestExecutionListener trigger() {
|
public TestExecutionListener listener() {
|
||||||
return mock(TestExecutionListener.class);
|
return mock(TestExecutionListener.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeTestClass
|
@BeforeTestClass
|
||||||
public void beforeTestClass(BeforeTestClassEvent e) throws Exception {
|
public void beforeTestClass(BeforeTestClassEvent e) throws Exception {
|
||||||
trigger().beforeTestClass(e.getSource());
|
listener().beforeTestClass(e.getSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
@PrepareTestInstance
|
@PrepareTestInstance
|
||||||
public void prepareTestInstance(PrepareTestInstanceEvent e) throws Exception {
|
public void prepareTestInstance(PrepareTestInstanceEvent e) throws Exception {
|
||||||
trigger().prepareTestInstance(e.getSource());
|
listener().prepareTestInstance(e.getSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeTestMethod
|
@BeforeTestMethod
|
||||||
public void beforeTestMethod(BeforeTestMethodEvent e) throws Exception {
|
public void beforeTestMethod(BeforeTestMethodEvent e) throws Exception {
|
||||||
trigger().beforeTestMethod(e.getSource());
|
listener().beforeTestMethod(e.getSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeTestExecution
|
@BeforeTestExecution
|
||||||
public void beforeTestExecutiob(BeforeTestExecutionEvent e) throws Exception {
|
public void beforeTestExecution(BeforeTestExecutionEvent e) throws Exception {
|
||||||
trigger().beforeTestExecution(e.getSource());
|
listener().beforeTestExecution(e.getSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterTestExecution
|
@AfterTestExecution
|
||||||
public void afterTestExecution(AfterTestExecutionEvent e) throws Exception {
|
public void afterTestExecution(AfterTestExecutionEvent e) throws Exception {
|
||||||
trigger().afterTestExecution(e.getSource());
|
listener().afterTestExecution(e.getSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterTestMethod
|
@AfterTestMethod
|
||||||
public void afterTestMethod(AfterTestMethodEvent e) throws Exception {
|
public void afterTestMethod(AfterTestMethodEvent e) throws Exception {
|
||||||
trigger().afterTestMethod(e.getSource());
|
listener().afterTestMethod(e.getSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterTestClass
|
@AfterTestClass
|
||||||
public void afterTestClass(AfterTestClassEvent e) throws Exception {
|
public void afterTestClass(AfterTestClassEvent e) throws Exception {
|
||||||
trigger().afterTestClass(e.getSource());
|
listener().afterTestClass(e.getSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration(classes = EventCaptureConfiguration.class)
|
@ContextConfiguration(classes = EventCaptureConfiguration.class)
|
||||||
@TestExecutionListeners(EventPublishingTestExecutionListener.class)
|
@TestExecutionListeners(EventPublishingTestExecutionListener.class)
|
||||||
static class EmptyTestCase {
|
static class TestCase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Serves as dummy test method.
|
* Serves as dummy test method.
|
||||||
|
|
@ -170,15 +165,4 @@ public class EventPublishingTestExecutionListenerIntegrationTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class TestContextExposingTestContextManager extends TestContextManager {
|
|
||||||
|
|
||||||
public TestContextExposingTestContextManager() {
|
|
||||||
super(EmptyTestCase.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TestContext getProtectedTestContext() {
|
|
||||||
return getTestContext();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,13 +23,14 @@ import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.Captor;
|
import org.mockito.Captor;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
import org.mockito.junit.MockitoJUnitRunner;
|
||||||
|
|
||||||
import org.springframework.test.context.TestContext;
|
import org.springframework.test.context.TestContext;
|
||||||
|
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.instanceOf;
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
import static org.mockito.BDDMockito.only;
|
import static org.mockito.Mockito.only;
|
||||||
import static org.mockito.BDDMockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link EventPublishingTestExecutionListener}.
|
* Unit tests for {@link EventPublishingTestExecutionListener}.
|
||||||
|
|
@ -40,58 +41,58 @@ import static org.mockito.BDDMockito.verify;
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class EventPublishingTestExecutionListenerTests {
|
public class EventPublishingTestExecutionListenerTests {
|
||||||
|
|
||||||
|
private final EventPublishingTestExecutionListener listener = new EventPublishingTestExecutionListener();
|
||||||
|
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private TestContext testContext;
|
private TestContext testContext;
|
||||||
|
|
||||||
@Captor
|
@Captor
|
||||||
private ArgumentCaptor<TestContextEvent> testExecutionEvent;
|
private ArgumentCaptor<TestContextEvent> testExecutionEvent;
|
||||||
|
|
||||||
private final EventPublishingTestExecutionListener listener = new EventPublishingTestExecutionListener();
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void publishBeforeClassTestExecutionEvent() {
|
public void publishBeforeTestClassEvent() {
|
||||||
listener.beforeTestClass(testContext);
|
listener.beforeTestClass(testContext);
|
||||||
assertEvent(BeforeTestClassEvent.class);
|
assertEvent(BeforeTestClassEvent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void publishPrepareInstanceTestExecutionEvent() {
|
public void publishPrepareTestInstanceEvent() {
|
||||||
listener.prepareTestInstance(testContext);
|
listener.prepareTestInstance(testContext);
|
||||||
assertEvent(PrepareTestInstanceEvent.class);
|
assertEvent(PrepareTestInstanceEvent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void publishBeforeMethodTestExecutionEvent() {
|
public void publishBeforeTestMethodEvent() {
|
||||||
listener.beforeTestMethod(testContext);
|
listener.beforeTestMethod(testContext);
|
||||||
assertEvent(BeforeTestMethodEvent.class);
|
assertEvent(BeforeTestMethodEvent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void publishBeforeExecutionTestExecutionEvent() {
|
public void publishBeforeTestExecutionEvent() {
|
||||||
listener.beforeTestExecution(testContext);
|
listener.beforeTestExecution(testContext);
|
||||||
assertEvent(BeforeTestExecutionEvent.class);
|
assertEvent(BeforeTestExecutionEvent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void publishAfterExecutionTestExecutionEvent() {
|
public void publishAfterTestExecutionEvent() {
|
||||||
listener.afterTestExecution(testContext);
|
listener.afterTestExecution(testContext);
|
||||||
assertEvent(AfterTestExecutionEvent.class);
|
assertEvent(AfterTestExecutionEvent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void publishAfterMethodTestExecutionEvent() {
|
public void publishAfterTestMethodEvent() {
|
||||||
listener.afterTestMethod(testContext);
|
listener.afterTestMethod(testContext);
|
||||||
assertEvent(AfterTestMethodEvent.class);
|
assertEvent(AfterTestMethodEvent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void publishAfterClassTestExecutionEvent() {
|
public void publishAfterTestClassEvent() {
|
||||||
listener.afterTestClass(testContext);
|
listener.afterTestClass(testContext);
|
||||||
assertEvent(AfterTestClassEvent.class);
|
assertEvent(AfterTestClassEvent.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends TestContextEvent> void assertEvent(Class<T> eventClass) {
|
private void assertEvent(Class<? extends TestContextEvent> eventClass) {
|
||||||
verify(testContext.getApplicationContext(), only()).publishEvent(testExecutionEvent.capture());
|
verify(testContext.getApplicationContext(), only()).publishEvent(testExecutionEvent.capture());
|
||||||
assertThat(testExecutionEvent.getValue(), instanceOf(eventClass));
|
assertThat(testExecutionEvent.getValue(), instanceOf(eventClass));
|
||||||
assertThat(testExecutionEvent.getValue().getSource(), equalTo(testContext));
|
assertThat(testExecutionEvent.getValue().getSource(), equalTo(testContext));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue