2
2
3
3
import io .cucumber .core .backend .CucumberBackendException ;
4
4
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
5
+ import org .springframework .beans .factory .config .Scope ;
5
6
import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
6
7
import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
7
8
import org .springframework .context .ConfigurableApplicationContext ;
13
14
14
15
import static io .cucumber .spring .CucumberTestContext .SCOPE_CUCUMBER_GLUE ;
15
16
16
- abstract class TestContextAdaptor {
17
+ class TestContextAdaptor {
17
18
18
19
private static final Object monitor = new Object ();
19
20
20
21
private final TestContextManager delegate ;
21
22
private final ConfigurableApplicationContext applicationContext ;
22
23
private final Collection <Class <?>> glueClasses ;
23
24
24
- protected TestContextAdaptor (
25
- TestContextManager delegate ,
26
- ConfigurableApplicationContext applicationContext ,
27
- Collection <Class <?>> glueClasses
28
- ) {
29
- this .delegate = delegate ;
30
- this .applicationContext = applicationContext ;
31
- this .glueClasses = glueClasses ;
32
- }
33
-
34
- static TestContextAdaptor createTestContextManagerAdaptor (
25
+ TestContextAdaptor (
35
26
TestContextManager delegate ,
36
27
Collection <Class <?>> glueClasses
37
28
) {
38
29
TestContext testContext = delegate .getTestContext ();
39
30
ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext ) testContext
40
31
.getApplicationContext ();
41
- return new TestContextAdaptor ( delegate , applicationContext , glueClasses ) {
42
-
43
- } ;
32
+ this . delegate = delegate ;
33
+ this . applicationContext = applicationContext ;
34
+ this . glueClasses = glueClasses ;
44
35
}
45
36
46
37
public final void start () {
@@ -50,15 +41,15 @@ public final void start() {
50
41
// #1153, #1148, #1106) we do this serially.
51
42
synchronized (monitor ) {
52
43
registerGlueCodeScope (applicationContext );
53
- notifyContextManagerAboutTestClassStarted ();
54
44
registerStepClassBeanDefinitions (applicationContext .getBeanFactory ());
55
45
}
46
+ notifyContextManagerAboutBeforeTestClass ();
47
+ CucumberTestContext .getInstance ().start ();
56
48
notifyTestContextManagerAboutBeforeTestMethod ();
57
49
}
58
50
59
51
private void notifyTestContextManagerAboutBeforeTestMethod () {
60
52
try {
61
- CucumberTestContext .getInstance ().start ();
62
53
Class <?> testClass = delegate .getTestContext ().getTestClass ();
63
54
Object testContextInstance = applicationContext .getBean (testClass );
64
55
Method dummyMethod = TestContextAdaptor .class .getMethod ("cucumberDoesNotHaveASingleTestMethod" );
@@ -70,12 +61,18 @@ private void notifyTestContextManagerAboutBeforeTestMethod() {
70
61
71
62
final void registerGlueCodeScope (ConfigurableApplicationContext context ) {
72
63
while (context != null ) {
73
- context .getBeanFactory ().registerScope (SCOPE_CUCUMBER_GLUE , new CucumberScenarioScope ());
64
+ ConfigurableListableBeanFactory beanFactory = context .getBeanFactory ();
65
+ // Scenario scope may have already been registered by another
66
+ // thread.
67
+ Scope registeredScope = beanFactory .getRegisteredScope (SCOPE_CUCUMBER_GLUE );
68
+ if (registeredScope == null ) {
69
+ beanFactory .registerScope (SCOPE_CUCUMBER_GLUE , new CucumberScenarioScope ());
70
+ }
74
71
context = (ConfigurableApplicationContext ) context .getParent ();
75
72
}
76
73
}
77
74
78
- private void notifyContextManagerAboutTestClassStarted () {
75
+ private void notifyContextManagerAboutBeforeTestClass () {
79
76
try {
80
77
delegate .beforeTestClass ();
81
78
} catch (Exception e ) {
@@ -106,6 +103,10 @@ private void registerStepClassBeanDefinition(BeanDefinitionRegistry registry, Cl
106
103
public final void stop () {
107
104
notifyTestContextManagerAboutAfterTestMethod ();
108
105
CucumberTestContext .getInstance ().stop ();
106
+ notifyTestContextManagerAboutAfterTestClass ();
107
+ }
108
+
109
+ private void notifyTestContextManagerAboutAfterTestClass () {
109
110
try {
110
111
delegate .afterTestClass ();
111
112
} catch (Exception e ) {
@@ -115,7 +116,6 @@ public final void stop() {
115
116
116
117
private void notifyTestContextManagerAboutAfterTestMethod () {
117
118
try {
118
- CucumberTestContext .getInstance ().start ();
119
119
Class <?> testClass = delegate .getTestContext ().getTestClass ();
120
120
Object testContextInstance = applicationContext .getBean (testClass );
121
121
Method dummyMethod = TestContextAdaptor .class .getMethod ("cucumberDoesNotHaveASingleTestMethod" );
0 commit comments