Skip to content

Commit 91deb2a

Browse files
authored
[Spring] Do not invoke after test method if test failed to start (#2585)
1 parent 4f0cc30 commit 91deb2a

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2020

2121
### Fixed
2222
* [Java] Process glue classes distinctly ([#2582](https://github.com/cucumber/cucumber-jvm/pull/2582) M.P. Korstanje)
23+
* [Spring] Do not invoke after test methods if test failed to start ([#2585](https://github.com/cucumber/cucumber-jvm/pull/2585) M.P. Korstanje)
2324

2425
## [7.4.1] (2022-06-23)
2526

spring/src/main/java/io/cucumber/spring/TestContextAdaptor.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TestContextAdaptor {
2121
private final TestContextManager delegate;
2222
private final ConfigurableApplicationContext applicationContext;
2323
private final Collection<Class<?>> glueClasses;
24+
private Object delegateTestInstance;
2425

2526
TestContextAdaptor(
2627
TestContextManager delegate,
@@ -51,7 +52,7 @@ public final void start() {
5152
private void notifyTestContextManagerAboutBeforeTestMethod() {
5253
try {
5354
Class<?> delegateTestClass = delegate.getTestContext().getTestClass();
54-
Object delegateTestInstance = applicationContext.getBean(delegateTestClass);
55+
delegateTestInstance = applicationContext.getBean(delegateTestClass);
5556
Method dummyMethod = TestContextAdaptor.class.getMethod("cucumberDoesNotHaveASingleTestMethod");
5657
delegate.beforeTestMethod(delegateTestInstance, dummyMethod);
5758
} catch (Exception e) {
@@ -106,7 +107,10 @@ public final void stop() {
106107
// session. This is not ideal, but Cucumber only supports 1 set of
107108
// before/after semantics while JUnit and Spring have 2 sets.
108109
if (CucumberTestContext.getInstance().isActive()) {
109-
notifyTestContextManagerAboutAfterTestMethod();
110+
if(delegateTestInstance != null) {
111+
notifyTestContextManagerAboutAfterTestMethod();
112+
delegateTestInstance = null;
113+
}
110114
CucumberTestContext.getInstance().stop();
111115
}
112116
notifyTestContextManagerAboutAfterTestClass();

spring/src/test/java/io/cucumber/spring/SpringFactoryTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ void shouldBeStoppableWhenFacedWithMissingContextConfiguration() {
349349
@ValueSource(classes = {
350350
FailedBeforeTestClassContextConfiguration.class,
351351
FailedBeforeTestMethodContextConfiguration.class,
352+
FailedTestInstanceContextConfiguration.class
352353
})
353354
void shouldBeStoppableWhenFacedWithFailedApplicationContext(Class<?> contextConfiguration) {
354355
final ObjectFactory factory = new SpringFactory();
@@ -424,6 +425,14 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
424425
}
425426

426427
}
428+
@CucumberContextConfiguration
429+
@ContextConfiguration("classpath:cucumber.xml")
430+
public static class FailedTestInstanceContextConfiguration {
431+
432+
public FailedTestInstanceContextConfiguration(){
433+
throw new RuntimeException();
434+
}
435+
}
427436

428437
public static class StubException extends Exception {
429438

0 commit comments

Comments
 (0)