Skip to content

Commit 6d5a4d5

Browse files
marschallfmbenhassine
authored andcommitted
Remove Reflection from StepScopeTestExecutionListener
Call methods on TestContext directly
1 parent 31899d5 commit 6d5a4d5

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

spring-batch-test/src/main/java/org/springframework/batch/test/StepScopeTestExecutionListener.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2014 the original author or authors.
2+
* Copyright 2006-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,10 +65,6 @@
6565
*/
6666
public class StepScopeTestExecutionListener implements TestExecutionListener {
6767
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";
7268

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

8480
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);
8782
}
8883
}
8984

@@ -94,12 +89,9 @@ public void prepareTestInstance(TestContext testContext) throws Exception {
9489
*/
9590
@Override
9691
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);
9992

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);
10395

10496
StepSynchronizationManager.register(stepExecution);
10597
}
@@ -112,10 +104,8 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
112104
*/
113105
@Override
114106
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);
117107

118-
if (hasAttribute) {
108+
if (testContext.hasAttribute(STEP_EXECUTION)) {
119109
StepSynchronizationManager.close();
120110
}
121111
}
@@ -142,14 +132,7 @@ public void beforeTestClass(TestContext testContext) throws Exception {
142132
* @return a {@link StepExecution}
143133
*/
144134
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();
153136

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

0 commit comments

Comments
 (0)