Search in sources :

Example 11 with ContextCustomizer

use of org.springframework.test.context.ContextCustomizer in project spring-boot by spring-projects.

the class ImportsContextCustomizerFactoryTests method getContextCustomizerWhenHasImportAnnotationShouldReturnCustomizer.

@Test
public void getContextCustomizerWhenHasImportAnnotationShouldReturnCustomizer() {
    ContextCustomizer customizer = this.factory.createContextCustomizer(TestWithImport.class, null);
    assertThat(customizer).isNotNull();
}
Also used : ContextCustomizer(org.springframework.test.context.ContextCustomizer) Test(org.junit.Test)

Example 12 with ContextCustomizer

use of org.springframework.test.context.ContextCustomizer in project spring-boot by spring-projects.

the class ImportsContextCustomizerFactoryTests method contextCustomizerEqualsAndHashCode.

@Test
public void contextCustomizerEqualsAndHashCode() throws Exception {
    ContextCustomizer customizer1 = this.factory.createContextCustomizer(TestWithImport.class, null);
    ContextCustomizer customizer2 = this.factory.createContextCustomizer(TestWithImport.class, null);
    ContextCustomizer customizer3 = this.factory.createContextCustomizer(TestWithImportAndMetaImport.class, null);
    ContextCustomizer customizer4 = this.factory.createContextCustomizer(TestWithSameImportAndMetaImport.class, null);
    assertThat(customizer1.hashCode()).isEqualTo(customizer1.hashCode());
    assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode());
    assertThat(customizer1).isEqualTo(customizer1).isEqualTo(customizer2).isNotEqualTo(customizer3);
    assertThat(customizer3).isEqualTo(customizer4);
}
Also used : ContextCustomizer(org.springframework.test.context.ContextCustomizer) Test(org.junit.Test)

Example 13 with ContextCustomizer

use of org.springframework.test.context.ContextCustomizer in project spring-boot by spring-projects.

the class MockitoContextCustomizerFactoryTests method getContextCustomizerUsesMocksAsCacheKey.

@Test
public void getContextCustomizerUsesMocksAsCacheKey() throws Exception {
    ContextCustomizer customizer = this.factory.createContextCustomizer(WithMockBeanAnnotation.class, null);
    assertThat(customizer).isNotNull();
    ContextCustomizer same = this.factory.createContextCustomizer(WithSameMockBeanAnnotation.class, null);
    assertThat(customizer).isNotNull();
    ContextCustomizer different = this.factory.createContextCustomizer(WithDifferentMockBeanAnnotation.class, null);
    assertThat(different).isNotNull();
    assertThat(customizer.hashCode()).isEqualTo(same.hashCode());
    assertThat(customizer.hashCode()).isNotEqualTo(different.hashCode());
    assertThat(customizer).isEqualTo(customizer);
    assertThat(customizer).isEqualTo(same);
    assertThat(customizer).isNotEqualTo(different);
}
Also used : ContextCustomizer(org.springframework.test.context.ContextCustomizer) Test(org.junit.Test)

Example 14 with ContextCustomizer

use of org.springframework.test.context.ContextCustomizer in project spring-boot by spring-projects.

the class MockitoContextCustomizerFactoryTests method getContextCustomizerWithoutAnnotationReturnsCustomizer.

@Test
public void getContextCustomizerWithoutAnnotationReturnsCustomizer() throws Exception {
    ContextCustomizer customizer = this.factory.createContextCustomizer(NoMockBeanAnnotation.class, null);
    assertThat(customizer).isNotNull();
}
Also used : ContextCustomizer(org.springframework.test.context.ContextCustomizer) Test(org.junit.Test)

Example 15 with ContextCustomizer

use of org.springframework.test.context.ContextCustomizer in project spring-framework by spring-projects.

the class AbstractTestContextBootstrapper method buildMergedContextConfiguration.

/**
	 * Build the {@link MergedContextConfiguration merged context configuration}
	 * for the supplied {@link Class testClass}, context configuration attributes,
	 * and parent context configuration.
	 * @param testClass the test class for which the {@code MergedContextConfiguration}
	 * should be built (must not be {@code null})
	 * @param configAttributesList the list of context configuration attributes for the
	 * specified test class, ordered <em>bottom-up</em> (i.e., as if we were
	 * traversing up the class hierarchy); never {@code null} or empty
	 * @param parentConfig the merged context configuration for the parent application
	 * context in a context hierarchy, or {@code null} if there is no parent
	 * @param cacheAwareContextLoaderDelegate the cache-aware context loader delegate to
	 * be passed to the {@code MergedContextConfiguration} constructor
	 * @param requireLocationsClassesOrInitializers whether locations, classes, or
	 * initializers are required; typically {@code true} but may be set to {@code false}
	 * if the configured loader supports empty configuration
	 * @return the merged context configuration
	 * @see #resolveContextLoader
	 * @see ContextLoaderUtils#resolveContextConfigurationAttributes
	 * @see SmartContextLoader#processContextConfiguration
	 * @see ContextLoader#processLocations
	 * @see ActiveProfilesUtils#resolveActiveProfiles
	 * @see ApplicationContextInitializerUtils#resolveInitializerClasses
	 * @see MergedContextConfiguration
	 */
private MergedContextConfiguration buildMergedContextConfiguration(Class<?> testClass, List<ContextConfigurationAttributes> configAttributesList, MergedContextConfiguration parentConfig, CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate, boolean requireLocationsClassesOrInitializers) {
    Assert.notEmpty(configAttributesList, "ContextConfigurationAttributes list must not be null or empty");
    ContextLoader contextLoader = resolveContextLoader(testClass, configAttributesList);
    List<String> locations = new ArrayList<>();
    List<Class<?>> classes = new ArrayList<>();
    List<Class<?>> initializers = new ArrayList<>();
    for (ContextConfigurationAttributes configAttributes : configAttributesList) {
        if (logger.isTraceEnabled()) {
            logger.trace(String.format("Processing locations and classes for context configuration attributes %s", configAttributes));
        }
        if (contextLoader instanceof SmartContextLoader) {
            SmartContextLoader smartContextLoader = (SmartContextLoader) contextLoader;
            smartContextLoader.processContextConfiguration(configAttributes);
            locations.addAll(0, Arrays.asList(configAttributes.getLocations()));
            classes.addAll(0, Arrays.asList(configAttributes.getClasses()));
        } else {
            String[] processedLocations = contextLoader.processLocations(configAttributes.getDeclaringClass(), configAttributes.getLocations());
            locations.addAll(0, Arrays.asList(processedLocations));
        // Legacy ContextLoaders don't know how to process classes
        }
        initializers.addAll(0, Arrays.asList(configAttributes.getInitializers()));
        if (!configAttributes.isInheritLocations()) {
            break;
        }
    }
    Set<ContextCustomizer> contextCustomizers = getContextCustomizers(testClass, Collections.unmodifiableList(configAttributesList));
    Assert.state(!(requireLocationsClassesOrInitializers && areAllEmpty(locations, classes, initializers, contextCustomizers)), () -> String.format("%s was unable to detect defaults, and no ApplicationContextInitializers " + "or ContextCustomizers were declared for context configuration attributes %s", contextLoader.getClass().getSimpleName(), configAttributesList));
    MergedTestPropertySources mergedTestPropertySources = TestPropertySourceUtils.buildMergedTestPropertySources(testClass);
    MergedContextConfiguration mergedConfig = new MergedContextConfiguration(testClass, StringUtils.toStringArray(locations), ClassUtils.toClassArray(classes), ApplicationContextInitializerUtils.resolveInitializerClasses(configAttributesList), ActiveProfilesUtils.resolveActiveProfiles(testClass), mergedTestPropertySources.getLocations(), mergedTestPropertySources.getProperties(), contextCustomizers, contextLoader, cacheAwareContextLoaderDelegate, parentConfig);
    return processMergedContextConfiguration(mergedConfig);
}
Also used : MergedContextConfiguration(org.springframework.test.context.MergedContextConfiguration) ContextConfigurationAttributes(org.springframework.test.context.ContextConfigurationAttributes) ArrayList(java.util.ArrayList) SmartContextLoader(org.springframework.test.context.SmartContextLoader) SmartContextLoader(org.springframework.test.context.SmartContextLoader) ContextLoader(org.springframework.test.context.ContextLoader) ContextCustomizer(org.springframework.test.context.ContextCustomizer)

Aggregations

ContextCustomizer (org.springframework.test.context.ContextCustomizer)25 Test (org.junit.Test)22 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 ArrayList (java.util.ArrayList)2 MergedContextConfiguration (org.springframework.test.context.MergedContextConfiguration)2 LinkedHashSet (java.util.LinkedHashSet)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 TypeExcludeFilter (org.springframework.boot.context.TypeExcludeFilter)1 ServletContextApplicationContextInitializer (org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer)1 ApplicationContextInitializer (org.springframework.context.ApplicationContextInitializer)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 MetadataReader (org.springframework.core.type.classreading.MetadataReader)1 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)1 SimpleMetadataReaderFactory (org.springframework.core.type.classreading.SimpleMetadataReaderFactory)1 ContextConfigurationAttributes (org.springframework.test.context.ContextConfigurationAttributes)1 ContextCustomizerFactory (org.springframework.test.context.ContextCustomizerFactory)1 ContextLoader (org.springframework.test.context.ContextLoader)1 SmartContextLoader (org.springframework.test.context.SmartContextLoader)1