本文整理汇总了Java中org.springframework.test.context.TestContext.getApplicationContext方法的典型用法代码示例。如果您正苦于以下问题:Java TestContext.getApplicationContext方法的具体用法?Java TestContext.getApplicationContext怎么用?Java TestContext.getApplicationContext使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.test.context.TestContext
的用法示例。
在下文中一共展示了TestContext.getApplicationContext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: optimizedDbReset
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
private void optimizedDbReset(TestContext testContext, FlywayTest annotation) throws Exception {
if (annotation != null && annotation.invokeCleanDB() && annotation.invokeMigrateDB() && !annotation.invokeBaselineDB()) {
ApplicationContext applicationContext = testContext.getApplicationContext();
FlywayDataSourceContext dataSourceContext = getDataSourceContext(applicationContext, annotation.flywayName());
Flyway flywayBean = ReflectionTestUtils.invokeMethod(this, "getBean", applicationContext, Flyway.class, annotation.flywayName());
if (dataSourceContext != null && flywayBean != null) {
prepareDataSourceContext(dataSourceContext, flywayBean, annotation);
FlywayTest adjustedAnnotation = copyAnnotation(annotation, false, false, true);
ReflectionTestUtils.invokeMethod(this, "dbResetWithAnnotation", testContext, adjustedAnnotation);
return;
}
}
ReflectionTestUtils.invokeMethod(this, "dbResetWithAnnotation", testContext, annotation);
}
开发者ID:zonkyio,项目名称:embedded-database-spring-test,代码行数:21,代码来源:OptimizedFlywayTestExecutionListener.java
示例2: resetMocks
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
private void resetMocks(TestContext testContext, Predicate<MockitoDoubleConfiguration> shouldResetPredicate) throws Exception {
final ApplicationContext applicationContext = testContext.getApplicationContext();
final DoubleRegistry doubleRegistry = applicationContext.getBean(DoubleRegistry.BEAN_NAME, DoubleRegistry.class);
final DoubleSearch doubleSearch = doubleRegistry.doublesSearch();
for (DoubleDefinition doubleDefinition : doubleRegistry.doublesSearch()) {
final String beanName = doubleDefinition.getName();
final Object bean = applicationContext.getBean(beanName);
if (Mockito.mockingDetails(bean).isMock()) {
final DoubleDefinition definition = doubleSearch.findOneDefinition(beanName);
final MockitoDoubleConfiguration configuration = definition.getConfiguration(MockitoDoubleConfiguration.class);
if (shouldResetPredicate.test(configuration)) {
mockResetExecutor.resetMock(bean);
}
}
}
}
示例3: 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);
}
示例4: removeContextHierarchyCacheLevel1
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Test
public void removeContextHierarchyCacheLevel1() {
// Load Level 3-A
TestContext testContext3a = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 1
// Should also remove Levels 2, 3-A, and 3-B, leaving nothing.
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent().getParent(),
HierarchyMode.CURRENT_LEVEL);
assertContextCacheStatistics(contextCache, "removed level 1", 0, 1, 4);
assertParentContextCount(0);
}
示例5: removeContextHierarchyCacheLevel1WithExhaustiveMode
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Test
public void removeContextHierarchyCacheLevel1WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 1
// Should also remove Levels 2, 3-A, and 3-B, leaving nothing.
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent().getParent(),
HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 1", 0, 1, 4);
assertParentContextCount(0);
}
示例6: removeContextHierarchyCacheLevel2
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Test
public void removeContextHierarchyCacheLevel2() {
// Load Level 3-A
TestContext testContext3a = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 2
// Should also remove Levels 3-A and 3-B, leaving only Level 1 as a context in the
// cache but also removing the Level 1 hierarchy since all children have been
// removed.
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.CURRENT_LEVEL);
assertContextCacheStatistics(contextCache, "removed level 2", 1, 1, 4);
assertParentContextCount(0);
}
示例7: removeContextHierarchyCacheLevel2WithExhaustiveMode
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Test
public void removeContextHierarchyCacheLevel2WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 2
// Should wipe the cache
contextCache.remove(getMergedContextConfiguration(testContext3a).getParent(), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4);
assertParentContextCount(0);
}
示例8: 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);
}
}
}
示例9: afterTestClass
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void afterTestClass(TestContext testContext) throws Exception {
if(wireMockAnnotation == null) {
return;
}
ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) testContext
.getApplicationContext();
applicationContext.getBean(WireMockServer.class).stop();
}
示例10: prepareTestInstance
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
LOG.info("@RunWith(CamelSpringBootJUnit4ClassRunner.class) preparing: {}", testContext.getTestClass());
Class<?> testClass = testContext.getTestClass();
ConfigurableApplicationContext context = (ConfigurableApplicationContext) testContext.getApplicationContext();
// Post CamelContext(s) instantiation but pre CamelContext(s) start setup
CamelAnnotationsHandler.handleProvidesBreakpoint(context, testClass);
CamelAnnotationsHandler.handleShutdownTimeout(context, testClass);
CamelAnnotationsHandler.handleMockEndpoints(context, testClass);
CamelAnnotationsHandler.handleMockEndpointsAndSkip(context, testClass);
CamelAnnotationsHandler.handleUseOverridePropertiesWithPropertiesComponent(context, testClass);
}
示例11: removeContextHierarchyCacheLevel3Then2WithExhaustiveMode
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Test
public void removeContextHierarchyCacheLevel3Then2WithExhaustiveMode() {
// Load Level 3-A
TestContext testContext3a = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3aTestCase.class, contextCache);
testContext3a.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A", 3, 0, 3);
assertParentContextCount(2);
// Load Level 3-B
TestContext testContext3b = TestContextTestUtils.buildTestContext(
ClassHierarchyContextHierarchyLevel3bTestCase.class, contextCache);
testContext3b.getApplicationContext();
assertContextCacheStatistics(contextCache, "level 3, A and B", 4, 1, 4);
assertParentContextCount(2);
// Remove Level 3-A
// Should wipe the cache.
contextCache.remove(getMergedContextConfiguration(testContext3a), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 3-A", 0, 1, 4);
assertParentContextCount(0);
// Remove Level 2
// Should not actually do anything since the cache was cleared in the
// previous step. So the stats should remain the same.
contextCache.remove(getMergedContextConfiguration(testContext3b).getParent(), HierarchyMode.EXHAUSTIVE);
assertContextCacheStatistics(contextCache, "removed level 2", 0, 1, 4);
assertParentContextCount(0);
}
示例12: afterTestMethod
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
ApplicationContext applicationContext = testContext.getApplicationContext();
String[] names = applicationContext
.getBeanNamesForType(MockRestServiceServer.class, false, false);
for (String name : names) {
applicationContext.getBean(name, MockRestServiceServer.class).reset();
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:10,代码来源:MockRestServiceServerResetTestExecutionListener.java
示例13: prepareTestInstance
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
try {
this.delegate.prepareTestInstance(testContext);
}
catch (Exception ex) {
ApplicationContext context = testContext.getApplicationContext();
if (context instanceof ConfigurableApplicationContext) {
ConditionEvaluationReport report = ConditionEvaluationReport
.get(((ConfigurableApplicationContext) context).getBeanFactory());
System.err.println(new ConditionEvaluationReportMessage(report));
}
throw ex;
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:16,代码来源:AutoConfigureReportTestExecutionListener.java
示例14: injectFields
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
private void injectFields(TestContext testContext, DefinitionsParser parser) {
ApplicationContext applicationContext = testContext.getApplicationContext();
MockitoPostProcessor postProcessor = applicationContext
.getBean(MockitoPostProcessor.class);
for (Definition definition : parser.getDefinitions()) {
Field field = parser.getField(definition);
if (field != null) {
postProcessor.inject(field, testContext.getTestInstance(), definition);
}
}
}
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:12,代码来源:MockitoTestExecutionListener.java
示例15: prepareTestInstance
import org.springframework.test.context.TestContext; //导入方法依赖的package包/类
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {
if (testContext.getApplicationContext() instanceof GenericApplicationContext) {
GenericApplicationContext context = (GenericApplicationContext) testContext.getApplicationContext();
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
Scope requestScope = new SimpleThreadScope();
beanFactory.registerScope("request", requestScope);
Scope sessionScope = new SimpleThreadScope();
beanFactory.registerScope("session", sessionScope);
}
}