diff --git a/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java b/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java index 1d3f587bd2..52c9fe7cd2 100644 --- a/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java +++ b/spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java @@ -8,6 +8,7 @@ import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContextManager; +import java.lang.reflect.Method; import java.util.Collection; import static io.cucumber.spring.CucumberTestContext.SCOPE_CUCUMBER_GLUE; @@ -44,15 +45,28 @@ static TestContextAdaptor createTestContextManagerAdaptor( public final void start() { // The TestContextManager delegate makes the application context - // available to other threads. Register the glue however requires - // modifies the application context. To avoid concurrent modification - // issues (#1823, #1153, #1148, #1106) we do this serially. + // available to other threads. Registering the glue however modifies the + // application context. To avoid concurrent modification issues (#1823, + // #1153, #1148, #1106) we do this serially. synchronized (monitor) { registerGlueCodeScope(applicationContext); notifyContextManagerAboutTestClassStarted(); registerStepClassBeanDefinitions(applicationContext.getBeanFactory()); } - CucumberTestContext.getInstance().start(); + notifyTestContextManagerAboutBeforeTestMethod(); + } + + private void notifyTestContextManagerAboutBeforeTestMethod() { + try { + CucumberTestContext.getInstance().start(); + Class testClass = delegate.getTestContext().getTestClass(); + Object testContextInstance = applicationContext.getBean(testClass); + Method dummyMethod = TestContextAdaptor.class.getMethod("cucumberDoesNotHaveASingleTestMethod"); + delegate.beforeTestMethod(testContextInstance, dummyMethod); + delegate.beforeTestExecution(testContextInstance, dummyMethod); + } catch (Exception e) { + throw new CucumberBackendException(e.getMessage(), e); + } } final void registerGlueCodeScope(ConfigurableApplicationContext context) { @@ -91,6 +105,7 @@ private void registerStepClassBeanDefinition(BeanDefinitionRegistry registry, Cl } public final void stop() { + notifyTestContextManagerAboutAfterTestMethod(); CucumberTestContext.getInstance().stop(); try { delegate.afterTestClass(); @@ -99,8 +114,30 @@ public final void stop() { } } + + private void notifyTestContextManagerAboutAfterTestMethod() { + try { + CucumberTestContext.getInstance().start(); + Class testClass = delegate.getTestContext().getTestClass(); + Object testContextInstance = applicationContext.getBean(testClass); + Method dummyMethod = TestContextAdaptor.class.getMethod("cucumberDoesNotHaveASingleTestMethod"); + /* TODO: We need access to the scenario result here + This means we have to redesign the backend module with call backs in mind. + See JUnit 5 BeforeEachCallback, AfterEachCallback, + BeforeTestExecutionCallback, AfterTestExecutionCallback, + */ + delegate.afterTestExecution(testContextInstance, dummyMethod, null); + delegate.afterTestMethod(testContextInstance, dummyMethod, null); + } catch (Exception e) { + throw new CucumberBackendException(e.getMessage(), e); + } + } + final T getInstance(Class type) { return applicationContext.getBean(type); } + public void cucumberDoesNotHaveASingleTestMethod(){ + + } }