Skip to content

Remove Reflection from StepScopeTestExecutionListener #3908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@
*/
public class StepScopeTestExecutionListener implements TestExecutionListener {
private static final String STEP_EXECUTION = StepScopeTestExecutionListener.class.getName() + ".STEP_EXECUTION";
private static final String SET_ATTRIBUTE_METHOD_NAME = "setAttribute";
private static final String HAS_ATTRIBUTE_METHOD_NAME = "hasAttribute";
private static final String GET_ATTRIBUTE_METHOD_NAME = "getAttribute";
private static final String GET_TEST_INSTANCE_METHOD = "getTestInstance";

/**
* Set up a {@link StepExecution} as a test context attribute.
Expand All @@ -82,8 +78,7 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
StepExecution stepExecution = getStepExecution(testContext);

if (stepExecution != null) {
Method method = TestContext.class.getMethod(SET_ATTRIBUTE_METHOD_NAME, String.class, Object.class);
ReflectionUtils.invokeMethod(method, testContext, STEP_EXECUTION, stepExecution);
testContext.setAttribute(STEP_EXECUTION, stepExecution);
}
}

Expand All @@ -94,12 +89,9 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
*/
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
Method hasAttributeMethod = TestContext.class.getMethod(HAS_ATTRIBUTE_METHOD_NAME, String.class);
Boolean hasAttribute = (Boolean) ReflectionUtils.invokeMethod(hasAttributeMethod, testContext, STEP_EXECUTION);

if (hasAttribute) {
Method method = TestContext.class.getMethod(GET_ATTRIBUTE_METHOD_NAME, String.class);
StepExecution stepExecution = (StepExecution) ReflectionUtils.invokeMethod(method, testContext, STEP_EXECUTION);
if (testContext.hasAttribute(STEP_EXECUTION)) {
StepExecution stepExecution = (StepExecution) testContext.getAttribute(STEP_EXECUTION);

StepSynchronizationManager.register(stepExecution);
}
Expand All @@ -112,10 +104,8 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
*/
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
Method method = TestContext.class.getMethod(HAS_ATTRIBUTE_METHOD_NAME, String.class);
Boolean hasAttribute = (Boolean) ReflectionUtils.invokeMethod(method, testContext, STEP_EXECUTION);

if (hasAttribute) {
if (testContext.hasAttribute(STEP_EXECUTION)) {
StepSynchronizationManager.close();
}
}
Expand All @@ -142,14 +132,7 @@ public void beforeTestClass(TestContext testContext) throws Exception {
* @return a {@link StepExecution}
*/
protected StepExecution getStepExecution(TestContext testContext) {
Object target;

try {
Method method = TestContext.class.getMethod(GET_TEST_INSTANCE_METHOD);
target = ReflectionUtils.invokeMethod(method, testContext);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("No such method " + GET_TEST_INSTANCE_METHOD + " on provided TestContext", e);
}
Object target = testContext.getTestInstance();

ExtractorMethodCallback method = new ExtractorMethodCallback(StepExecution.class, "getStepExecution");
ReflectionUtils.doWithMethods(target.getClass(), method);
Expand Down