1
1
/*
2
- * Copyright 2006-2014 the original author or authors.
2
+ * Copyright 2006-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
65
65
*/
66
66
public class StepScopeTestExecutionListener implements TestExecutionListener {
67
67
private static final String STEP_EXECUTION = StepScopeTestExecutionListener .class .getName () + ".STEP_EXECUTION" ;
68
- private static final String SET_ATTRIBUTE_METHOD_NAME = "setAttribute" ;
69
- private static final String HAS_ATTRIBUTE_METHOD_NAME = "hasAttribute" ;
70
- private static final String GET_ATTRIBUTE_METHOD_NAME = "getAttribute" ;
71
- private static final String GET_TEST_INSTANCE_METHOD = "getTestInstance" ;
72
68
73
69
/**
74
70
* Set up a {@link StepExecution} as a test context attribute.
@@ -82,8 +78,7 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
82
78
StepExecution stepExecution = getStepExecution (testContext );
83
79
84
80
if (stepExecution != null ) {
85
- Method method = TestContext .class .getMethod (SET_ATTRIBUTE_METHOD_NAME , String .class , Object .class );
86
- ReflectionUtils .invokeMethod (method , testContext , STEP_EXECUTION , stepExecution );
81
+ testContext .setAttribute (STEP_EXECUTION , stepExecution );
87
82
}
88
83
}
89
84
@@ -94,12 +89,9 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
94
89
*/
95
90
@ Override
96
91
public void beforeTestMethod (TestContext testContext ) throws Exception {
97
- Method hasAttributeMethod = TestContext .class .getMethod (HAS_ATTRIBUTE_METHOD_NAME , String .class );
98
- Boolean hasAttribute = (Boolean ) ReflectionUtils .invokeMethod (hasAttributeMethod , testContext , STEP_EXECUTION );
99
92
100
- if (hasAttribute ) {
101
- Method method = TestContext .class .getMethod (GET_ATTRIBUTE_METHOD_NAME , String .class );
102
- StepExecution stepExecution = (StepExecution ) ReflectionUtils .invokeMethod (method , testContext , STEP_EXECUTION );
93
+ if (testContext .hasAttribute (STEP_EXECUTION )) {
94
+ StepExecution stepExecution = (StepExecution ) testContext .getAttribute (STEP_EXECUTION );
103
95
104
96
StepSynchronizationManager .register (stepExecution );
105
97
}
@@ -112,10 +104,8 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
112
104
*/
113
105
@ Override
114
106
public void afterTestMethod (TestContext testContext ) throws Exception {
115
- Method method = TestContext .class .getMethod (HAS_ATTRIBUTE_METHOD_NAME , String .class );
116
- Boolean hasAttribute = (Boolean ) ReflectionUtils .invokeMethod (method , testContext , STEP_EXECUTION );
117
107
118
- if (hasAttribute ) {
108
+ if (testContext . hasAttribute ( STEP_EXECUTION ) ) {
119
109
StepSynchronizationManager .close ();
120
110
}
121
111
}
@@ -142,14 +132,7 @@ public void beforeTestClass(TestContext testContext) throws Exception {
142
132
* @return a {@link StepExecution}
143
133
*/
144
134
protected StepExecution getStepExecution (TestContext testContext ) {
145
- Object target ;
146
-
147
- try {
148
- Method method = TestContext .class .getMethod (GET_TEST_INSTANCE_METHOD );
149
- target = ReflectionUtils .invokeMethod (method , testContext );
150
- } catch (NoSuchMethodException e ) {
151
- throw new IllegalStateException ("No such method " + GET_TEST_INSTANCE_METHOD + " on provided TestContext" , e );
152
- }
135
+ Object target = testContext .getTestInstance ();
153
136
154
137
ExtractorMethodCallback method = new ExtractorMethodCallback (StepExecution .class , "getStepExecution" );
155
138
ReflectionUtils .doWithMethods (target .getClass (), method );
0 commit comments