Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -91,6 +105,7 @@ private void registerStepClassBeanDefinition(BeanDefinitionRegistry registry, Cl
}

public final void stop() {
notifyTestContextManagerAboutAfterTestMethod();
CucumberTestContext.getInstance().stop();
try {
delegate.afterTestClass();
Expand All @@ -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> T getInstance(Class<T> type) {
return applicationContext.getBean(type);
}

public void cucumberDoesNotHaveASingleTestMethod(){

}
}