@Override protected ContextLoader resolveContextLoader(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList) { Class<?>[] classes = getClasses(testClass); if (!ObjectUtils.isEmpty(classes)) { for (ContextConfigurationAttributes configAttributes : configAttributesList) { addConfigAttributesClasses(configAttributes, classes); } } return super.resolveContextLoader(testClass, configAttributesList); }
/** * Create a new {@link MergedContextConfiguration} with different classes. * @param mergedConfig the source config * @param classes the replacement classes * @return a new {@link MergedContextConfiguration} */ protected final MergedContextConfiguration createModifiedConfig( MergedContextConfiguration mergedConfig, Class<?>[] classes) { return createModifiedConfig(mergedConfig, classes, mergedConfig.getPropertySourceProperties()); }
protected void verifyConfiguration(Class<?> testClass) { SpringBootTest springBootTest = getAnnotation(testClass); if (springBootTest != null && (springBootTest.webEnvironment() == WebEnvironment.DEFINED_PORT || springBootTest.webEnvironment() == WebEnvironment.RANDOM_PORT) && getAnnotation(WebAppConfiguration.class, testClass) != null) { throw new IllegalStateException("@WebAppConfiguration should only be used " + "with @SpringBootTest when @SpringBootTest is configured with a " + "mock web environment. Please remove @WebAppConfiguration or " + "reconfigure @SpringBootTest."); } }
@Override protected MergedContextConfiguration processMergedContextConfiguration( MergedContextConfiguration mergedConfig) { Class<?>[] classes = getOrFindConfigurationClasses(mergedConfig); List<String> propertySourceProperties = getAndProcessPropertySourceProperties( mergedConfig); mergedConfig = createModifiedConfig(mergedConfig, classes, StringUtils.toStringArray(propertySourceProperties)); WebEnvironment webEnvironment = getWebEnvironment(mergedConfig.getTestClass()); if (webEnvironment != null && isWebEnvironmentSupported(mergedConfig)) { WebApplicationType webApplicationType = getWebApplicationType(mergedConfig); if (webApplicationType == WebApplicationType.SERVLET && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) { WebAppConfiguration webAppConfiguration = AnnotatedElementUtils .findMergedAnnotation(mergedConfig.getTestClass(), WebAppConfiguration.class); String resourceBasePath = (webAppConfiguration != null) ? webAppConfiguration.value() : "src/main/webapp"; mergedConfig = new WebMergedContextConfiguration(mergedConfig, resourceBasePath); } else if (webApplicationType == WebApplicationType.REACTIVE && (webEnvironment.isEmbedded() || webEnvironment == WebEnvironment.MOCK)) { return new ReactiveWebMergedContextConfiguration(mergedConfig); } } return mergedConfig; }
@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; }
BootstrapContext bootstrapContext = new DefaultBootstrapContext(testClass, delegate); SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper(); bootstrapper.setBootstrapContext(bootstrapContext); MergedContextConfiguration configuration = bootstrapper.buildMergedContextConfiguration();
protected Class<?>[] getOrFindConfigurationClasses( MergedContextConfiguration mergedConfig) { Class<?>[] classes = mergedConfig.getClasses(); if (containsNonTestComponent(classes) || mergedConfig.hasLocations()) { return classes; } Class<?> found = new AnnotatedClassFinder(SpringBootConfiguration.class) .findFromClass(mergedConfig.getTestClass()); Assert.state(found != null, "Unable to find a @SpringBootConfiguration, you need to use " + "@ContextConfiguration or @SpringBootTest(classes=...) " + "with your test"); logger.info("Found @SpringBootConfiguration " + found.getName() + " for test " + mergedConfig.getTestClass()); return merge(found, classes); }
/** * Create a new {@link MergedContextConfiguration} with different classes and * properties. * @param mergedConfig the source config * @param classes the replacement classes * @param propertySourceProperties the replacement properties * @return a new {@link MergedContextConfiguration} */ protected final MergedContextConfiguration createModifiedConfig( MergedContextConfiguration mergedConfig, Class<?>[] classes, String[] propertySourceProperties) { return new MergedContextConfiguration(mergedConfig.getTestClass(), mergedConfig.getLocations(), classes, mergedConfig.getContextInitializerClasses(), mergedConfig.getActiveProfiles(), mergedConfig.getPropertySourceLocations(), propertySourceProperties, mergedConfig.getContextCustomizers(), mergedConfig.getContextLoader(), getCacheAwareContextLoaderDelegate(), mergedConfig.getParent()); }
protected Class<?>[] getClasses(Class<?> testClass) { SpringBootTest annotation = getAnnotation(testClass); return (annotation != null) ? annotation.classes() : null; }
/** * Return the {@link WebEnvironment} type for this test or null if undefined. * @param testClass the source test class * @return the {@link WebEnvironment} or {@code null} */ protected WebEnvironment getWebEnvironment(Class<?> testClass) { SpringBootTest annotation = getAnnotation(testClass); return (annotation != null) ? annotation.webEnvironment() : null; }
protected String[] getProperties(Class<?> testClass) { SpringBootTest annotation = getAnnotation(testClass); return (annotation != null) ? annotation.properties() : null; }