/** * Set up a {@link StepExecution} as a test context attribute. * * @param testContext the current test context * @throws Exception if there is a problem * @see TestExecutionListener#prepareTestInstance(TestContext) */ @Override 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); } }
@Test public void testDefaultStepContext() throws Exception { TestContext testContext = getTestContext(new Object()); listener.prepareTestInstance(testContext); listener.beforeTestMethod(testContext); StepContext context = StepSynchronizationManager.getContext(); assertNotNull(context); listener.afterTestMethod(testContext); assertNull(StepSynchronizationManager.getContext()); }
private void testJobParameters(Object target) throws Exception { TestContext testContext = getTestContext(target); listener.prepareTestInstance(testContext); try { listener.beforeTestMethod(testContext); StepContext context = StepSynchronizationManager.getContext(); assertNotNull(context); assertEquals("spam", context.getJobParameters().get("foo")); } finally { listener.afterTestMethod(testContext); } assertNull(StepSynchronizationManager.getContext()); }
private void testExecutionContext(Object target) throws Exception { TestContext testContext = getTestContext(target); listener.prepareTestInstance(testContext); try { listener.beforeTestMethod(testContext); StepContext context = StepSynchronizationManager.getContext(); assertNotNull(context); assertEquals("bar", context.getStepExecutionContext().get("foo")); } finally { listener.afterTestMethod(testContext); } assertNull(StepSynchronizationManager.getContext()); }