/** * Mark the {@linkplain ApplicationContext application context} of the supplied * {@linkplain TestContext test context} as * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty} * and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}. * @param testContext the test context whose application context should * be marked as dirty * @param hierarchyMode the context cache clearing mode to be applied if the * context is part of a hierarchy; may be {@code null} * @since 3.2.2 */ protected void dirtyContext(TestContext testContext, @Nullable HierarchyMode hierarchyMode) { testContext.markApplicationContextDirty(hierarchyMode); testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); }
/** * Set up a {@link JobExecution} 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 { JobExecution jobExecution = getJobExecution(testContext); if (jobExecution != null) { testContext.setAttribute(JOB_EXECUTION, jobExecution); } }
@Override public void beforeTestMethod(TestContext testContext) throws Exception { String name = testContext.getTestMethod().getName(); actualMethods.add(name); testContext.setAttribute("method", name); this.methodName.set(name); }
/** * 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); }
testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE); testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
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(); }
@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(); }
/** * Marks the {@link ApplicationContext application context} of the supplied {@link TestContext test context} as * {@link TestContext#markApplicationContextDirty() dirty}, and sets the * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE REINJECT_DEPENDENCIES_ATTRIBUTE} * in the test context to <code>true</code> . */ protected void reloadContext(TestContext testContext) { testContext.markApplicationContextDirty(); testContext .setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); }
/** * Mark the {@linkplain ApplicationContext application context} of the supplied * {@linkplain TestContext test context} as * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty} * and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}. * @param testContext the test context whose application context should * be marked as dirty * @param hierarchyMode the context cache clearing mode to be applied if the * context is part of a hierarchy; may be {@code null} * @since 3.2.2 */ protected void dirtyContext(TestContext testContext, HierarchyMode hierarchyMode) { testContext.markApplicationContextDirty(hierarchyMode); testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); }
/** * Mark the {@linkplain ApplicationContext application context} of the supplied * {@linkplain TestContext test context} as * {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty} * and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}. * @param testContext the test context whose application context should * be marked as dirty * @param hierarchyMode the context cache clearing mode to be applied if the * context is part of a hierarchy; may be {@code null} * @since 3.2.2 */ protected void dirtyContext(TestContext testContext, @Nullable HierarchyMode hierarchyMode) { testContext.markApplicationContextDirty(hierarchyMode); testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); }
@Override public void afterTestClass(final TestContext testContext) throws Exception { //System.err.println(String.format("TemporaryDatabaseExecutionListener.afterTestClass(%s)", testContext)); try { if (!m_createNewDatabases && m_database != null) { m_database.drop(); } } catch (Throwable t) { throw new Exception("Caught an exception while dropping the database at the end of the test: " + t, t); } finally { testContext.markApplicationContextDirty(HierarchyMode.CURRENT_LEVEL); testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); } }
@Override public void afterTestClass(final TestContext testContext) throws Exception { //System.err.println(String.format("TemporaryDatabaseExecutionListener.afterTestClass(%s)", testContext)); try { if (!m_createNewDatabases && m_database != null) { m_database.drop(); } } catch (Throwable t) { throw new Exception("Caught an exception while dropping the database at the end of the test: " + t, t); } finally { testContext.markApplicationContextDirty(HierarchyMode.CURRENT_LEVEL); testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); } }
private synchronized void setUpVaadinScopesIfNecessary(TestContext testContext) { if (notAnnotatedWithVaadinAppConfiguration(testContext) || alreadySetUpVaadinScopes(testContext)) { logger.debug("No need to set up Vaadin scopes for test context [{}]", testContext); return; } VaadinScopes.setUp(); testContext.setAttribute(SET_UP_SCOPES_ATTRIBUTE, Boolean.TRUE); }
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 synchronized void setUpVaadinScopesIfNecessary(TestContext testContext) { if (notAnnotatedWithVaadinAppConfiguration(testContext) || alreadySetUpVaadinScopes(testContext)) { logger.debug("No need to set up Vaadin scopes for test context [{}]", testContext); return; } VaadinScopes.setUp(); testContext.setAttribute(SET_UP_SCOPES_ATTRIBUTE, Boolean.TRUE); }
@Override public void afterTestMethod(TestContext testContext) throws Exception { WebDriverScope scope = WebDriverScope .getFrom(testContext.getApplicationContext()); if (scope != null && scope.reset()) { testContext.setAttribute( DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); } }
@Override public TestContext buildTestContext() { TestContext context = super.buildTestContext(); verifyConfiguration(context.getTestClass()); WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass()); if (webEnvironment == WebEnvironment.MOCK && deduceWebApplicationType() == WebApplicationType.SERVLET) { context.setAttribute(ACTIVATE_SERVLET_LISTENER, true); } else if (webEnvironment != null && webEnvironment.isEmbedded()) { context.setAttribute(ACTIVATE_SERVLET_LISTENER, false); } return context; }
@Override public void beforeTestMethod(TestContext testContext) throws Exception { RiderTestContext riderTestContext = SpringRiderTestContext.create(testContext); testContext.setAttribute(RIDER_TEST_CONTEXT, riderTestContext); RiderRunner riderRunner = new RiderRunner(); riderRunner.setup(riderTestContext); riderRunner.runBeforeTest(riderTestContext); }