private boolean alreadyPopulatedRequestContextHolder(TestContext testContext) { return Boolean.TRUE.equals(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)); }
/** * If the {@link #REINJECT_DEPENDENCIES_ATTRIBUTE} in the supplied * {@link TestContext test context} has a value of {@link Boolean#TRUE}, * this method will have the same effect as * {@link #prepareTestInstance(TestContext) prepareTestInstance()}; * otherwise, this method will have no effect. */ @Override public void beforeTestMethod(TestContext testContext) throws Exception { if (Boolean.TRUE.equals(testContext.getAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE))) { if (logger.isDebugEnabled()) { logger.debug("Reinjecting dependencies for test context [" + testContext + "]."); } injectDependencies(testContext); } }
private boolean isActivated(TestContext testContext) { return (Boolean.TRUE.equals(testContext.getAttribute(ACTIVATE_LISTENER)) || AnnotatedElementUtils.hasAnnotation(testContext.getTestClass(), WebAppConfiguration.class)); }
@Override public void afterTestMethod(TestContext testContext) throws Exception { assertEquals(this.methodName.get(), testContext.getAttribute("method")); }
/** * If the {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} in the supplied * {@code TestContext} has a value of {@link Boolean#TRUE}, this method will * (1) clean up thread-local state after each test method by {@linkplain * RequestContextHolder#resetRequestAttributes() resetting} Spring Web's * {@code RequestContextHolder} and (2) ensure that new mocks are injected * into the test instance for subsequent tests by setting the * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE} * in the test context to {@code true}. * <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and * {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently * removed from the test context, regardless of their values. * @see TestExecutionListener#afterTestMethod(TestContext) */ @Override public void afterTestMethod(TestContext testContext) throws Exception { if (Boolean.TRUE.equals(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE))) { if (logger.isDebugEnabled()) { logger.debug(String.format("Resetting RequestContextHolder for test context %s.", testContext)); } RequestContextHolder.resetRequestAttributes(); testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); } testContext.removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); testContext.removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); }
/** * @param testContext the current test context * @throws Exception if there is a problem * @see TestExecutionListener#beforeTestMethod(TestContext) */ @Override public void beforeTestMethod(org.springframework.test.context.TestContext testContext) throws Exception { if (testContext.hasAttribute(JOB_EXECUTION)) { JobExecution jobExecution = (JobExecution) testContext.getAttribute(JOB_EXECUTION); JobSynchronizationManager.register(jobExecution); } }
/** * @since 4.3 */ @Test public void activateListenerWithoutExistingRequestAttributes() throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(NoAtWebAppConfigWebTestCase.class); given(testContext.getAttribute(ServletTestExecutionListener.ACTIVATE_LISTENER)).willReturn(true); RequestContextHolder.resetRequestAttributes(); listener.beforeTestClass(testContext); assertRequestAttributesDoNotExist(); assertWebAppConfigTestCase(); }
@Test public void legacyWebTestCaseWithPresetRequestAttributes() throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class); listener.beforeTestClass(testContext); assertSetUpOutsideOfStelAttributeExists(); listener.prepareTestInstance(testContext); assertSetUpOutsideOfStelAttributeExists(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.beforeTestMethod(testContext); assertSetUpOutsideOfStelAttributeExists(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.afterTestMethod(testContext); verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); assertSetUpOutsideOfStelAttributeExists(); }
private void assertWebAppConfigTestCase() throws Exception { listener.prepareTestInstance(testContext); assertRequestAttributesExist(); assertSetUpOutsideOfStelAttributeDoesNotExist(); verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(Boolean.TRUE); listener.beforeTestMethod(testContext); assertRequestAttributesExist(); assertSetUpOutsideOfStelAttributeDoesNotExist(); verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); listener.afterTestMethod(testContext); verify(testContext).removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); verify(testContext).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); assertRequestAttributesDoNotExist(); }
@Test public void legacyWebTestCaseWithoutExistingRequestAttributes() throws Exception { BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(LegacyWebTestCase.class); RequestContextHolder.resetRequestAttributes(); assertRequestAttributesDoNotExist(); listener.beforeTestClass(testContext); listener.prepareTestInstance(testContext); assertRequestAttributesDoNotExist(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); given(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).willReturn(null); listener.beforeTestMethod(testContext); assertRequestAttributesDoNotExist(); verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); listener.afterTestMethod(testContext); verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE); assertRequestAttributesDoNotExist(); }
private MockedClientSenderInitializer getClientSenderInitializer(TestContext testContext, boolean throwOnEmpty) { if (testContext.getAttribute(CLIENT_INITIALIZER) == null) { final ApplicationContext appContext = getAppContext(testContext, throwOnEmpty); if (appContext != null) { testContext.setAttribute(CLIENT_INITIALIZER, appContext.getBean(MockedClientSenderInitializer.class)); } } return (MockedClientSenderInitializer) testContext.getAttribute(CLIENT_INITIALIZER); }
private ProcessingEngine getProcessingEngine(TestContext testContext, boolean throwOnEmpty) { if (testContext.getAttribute(PROCESSING_ENGINE) == null) { final ApplicationContext appContext = getAppContext(testContext, throwOnEmpty); if (appContext != null) { testContext.setAttribute(PROCESSING_ENGINE, appContext.getBean(ProcessingEngine.class)); } } return (ProcessingEngine) testContext.getAttribute(PROCESSING_ENGINE); }
private Class getTestClass(TestContext testContext) { final Class res = (Class) testContext.getAttribute(REAL_TEST_CLASS_ATTR); return (res != null) ? res : testContext.getTestClass(); }
@Override public void afterTestMethod(TestContext testContext) throws Exception { if (Boolean.TRUE.equals(testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) { LOGGER.debug("Cleaning and reloading server for test context [{}]", testContext); cleanServer(); startServer(testContext); } }
@Override public void afterTestMethod(TestContext testContext) throws Exception { if (Boolean.TRUE.equals(testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) { LOGGER.debug("Cleaning and reloading server for test context [{}]", testContext); cleanServer(); startServer(testContext); } }
@Override public void afterTestMethod(TestContext testContext) throws Exception { if (Boolean.TRUE.equals(testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) { LOGGER.debug("Cleaning and reloading server for test context [{}]", testContext); cleanServer(); startServer(testContext); } }
@Override public void afterTestMethod(TestContext testContext) throws Exception { if (Boolean.TRUE.equals(testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) { LOGGER.debug("Cleaning and reloading server for test context [{}]", testContext); cleanServer(); startServer(testContext); } }
@Override public void beforeTestMethod(TestContext testContext) throws Exception { if (Boolean.TRUE.equals(testContext.getAttribute( DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) { initMocks(testContext); reinjectFields(testContext); } }
private boolean isActivated(TestContext testContext) { return (Boolean.TRUE.equals(testContext.getAttribute(ACTIVATE_LISTENER)) || AnnotatedElementUtils.hasAnnotation(testContext.getTestClass(), WebAppConfiguration.class)); }
@Override public void afterTestMethod(TestContext testContext) throws Exception { RiderTestContext riderTestContext = (RiderTestContext) testContext.getAttribute(RIDER_TEST_CONTEXT); RiderRunner riderRunner = new RiderRunner(); try { riderRunner.runAfterTest(riderTestContext); } finally { riderRunner.teardown(riderTestContext); riderTestContext.getDataSetExecutor().getRiderDataSource().getConnection().close(); } } }