當前位置: 首頁>>代碼示例>>Java>>正文


Java TestContext.setAttribute方法代碼示例

本文整理匯總了Java中org.springframework.test.context.TestContext.setAttribute方法的典型用法代碼示例。如果您正苦於以下問題:Java TestContext.setAttribute方法的具體用法?Java TestContext.setAttribute怎麽用?Java TestContext.setAttribute使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.test.context.TestContext的用法示例。


在下文中一共展示了TestContext.setAttribute方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: beforeTestMethod

import org.springframework.test.context.TestContext; //導入方法依賴的package包/類
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
	final Object testInstance = testContext.getTestInstance();
	if (!(testInstance instanceof Specification)) {
		return;
	}

	final Specification specification = (Specification) testInstance;
	final List<Object> mocks = new LinkedList<>();
	final ApplicationContext applicationContext = testContext.getApplicationContext();
	final DoubleRegistry doubleRegistry = applicationContext.getBean(DoubleRegistry.BEAN_NAME, DoubleRegistry.class);

	for (DoubleDefinition doubleDefinition : doubleRegistry.doublesSearch()) {
		final Optional<Object> doubleBean = tryToGetBean(applicationContext, doubleDefinition);

		doubleBean.ifPresent(bean -> {
			mocks.add(bean);
			mockUtil.attachMock(bean, specification);
		});
	}

	testContext.setAttribute(MOCKED_BEANS_NAMES, mocks);
}
 
開發者ID:pchudzik,項目名稱:springmock,代碼行數:24,代碼來源:MockAttachingTestExecutionListener.java

示例2: createTestContextManager

import org.springframework.test.context.TestContext; //導入方法依賴的package包/類
/**
 * Creates a new {@link TestContextManager} for the supplied test class.
 * <p>
 * Can be overridden by subclasses.
 *
 * @param clazz
 *            the test class to be managed
 */
@Override
protected TestContextManager createTestContextManager(final Class<?> clazz) {
    return new TestContextManager(clazz) {
        @Override
        public void beforeTestClass() throws Exception {
            super.beforeTestClass();
        }

        @Override
        public void afterTestClass() throws Exception {
            // If we aren't caching the Spring context them mark it dirty so
            // it is destroyed.
            if (!cacheSpringContext) {
                final TestContext testContext = getTestContext();
                testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);
                testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE,
                        Boolean.TRUE);
            }

            super.afterTestClass();
        }
    };
}
 
開發者ID:gchq,項目名稱:stroom-proxy,代碼行數:32,代碼來源:StroomSpringJUnit4ClassRunner.java

示例3: setUpRequestContextIfNecessary

import org.springframework.test.context.TestContext; //導入方法依賴的package包/類
private void setUpRequestContextIfNecessary(TestContext testContext) {
	if (notAnnotatedWithWebAppConfiguration(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
		return;
	}

	ApplicationContext context = testContext.getApplicationContext();

	if (context instanceof WebApplicationContext) {
		WebApplicationContext wac = (WebApplicationContext) context;
		ServletContext servletContext = wac.getServletContext();
		Assert.state(servletContext instanceof MockServletContext, String.format(
			"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
			testContext));

		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
				testContext));
		}

		MockServletContext mockServletContext = (MockServletContext) servletContext;
		MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
		request.setAttribute(CREATED_BY_THE_TESTCONTEXT_FRAMEWORK, Boolean.TRUE);
		MockHttpServletResponse response = new MockHttpServletResponse();
		ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

		RequestContextHolder.setRequestAttributes(servletWebRequest);
		testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
		testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);

		if (wac instanceof ConfigurableApplicationContext) {
			@SuppressWarnings("resource")
			ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
			ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
			bf.registerResolvableDependency(MockHttpServletResponse.class, response);
			bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
		}
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:40,代碼來源:ServletTestExecutionListener.java

示例4: buildTestContext

import org.springframework.test.context.TestContext; //導入方法依賴的package包/類
@Override
public TestContext buildTestContext() {
	TestContext context = super.buildTestContext();
	WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
	if (webEnvironment == WebEnvironment.MOCK && hasWebEnvironmentClasses()) {
		context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
	}
	else if (webEnvironment != null && webEnvironment.isEmbedded()) {
		context.setAttribute(ACTIVATE_SERVLET_LISTENER, false);
	}
	return context;
}
 
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:13,代碼來源:SpringBootTestContextBootstrapper.java

示例5: afterTestMethod

import org.springframework.test.context.TestContext; //導入方法依賴的package包/類
/**
 * 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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:30,代碼來源:ServletTestExecutionListener.java

示例6: dirtyContext

import org.springframework.test.context.TestContext; //導入方法依賴的package包/類
/**
 * 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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:17,代碼來源:AbstractDirtiesContextTestExecutionListener.java


注:本文中的org.springframework.test.context.TestContext.setAttribute方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。