@Test public void getContextCustomizerShouldAddExcludeFilters() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(WithExcludeFilters.class, null); customizer.customizeContext(this.context, this.mergedContextConfiguration); this.context.refresh(); TypeExcludeFilter filter = this.context.getBean(TypeExcludeFilter.class); MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); MetadataReader metadataReader = metadataReaderFactory .getMetadataReader(NoAnnotation.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isFalse(); metadataReader = metadataReaderFactory .getMetadataReader(SimpleExclude.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue(); metadataReader = metadataReaderFactory .getMetadataReader(TestClassAwareExclude.class.getName()); assertThat(filter.match(metadataReader, metadataReaderFactory)).isTrue(); }
@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); }
@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); }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes) { AutoConfigureEmbeddedDatabase databaseAnnotation = AnnotatedElementUtils.findMergedAnnotation(testClass, AutoConfigureEmbeddedDatabase.class); if (databaseAnnotation != null && databaseAnnotation.type() == EmbeddedDatabaseType.POSTGRES && databaseAnnotation.replace() != Replace.NONE) { return new PreloadableEmbeddedPostgresContextCustomizer(databaseAnnotation); } return null; }
@Override protected ContextCustomizer createContextCustomizer(DoubleRegistry doubleRegistry) { return new MockContextCustomizer( MockitoDoubleFactory.class, doubleRegistry, Stream .of(new SimpleEntry<>(SkipMockitoBeansPostProcessing.BEAN_NAME, skipMockitoBeansPostProcessing())) .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))); }
@Override protected ContextCustomizer createContextCustomizer(DoubleRegistry doubleRegistry) { return new MockContextCustomizer( doubleFactoryCreator, doubleRegistry, Collections.emptyMap()); }
@Override protected ContextCustomizer createContextCustomizer(DoubleRegistry doubleRegistry) { return new MockContextCustomizer( SpockDoubleFactory.class, doubleRegistry, Stream .of(new SimpleEntry<>(SkipSpockBeansPostProcessing.BEAN_NAME, skipSpockBeansPostProcessing())) .collect(toMap(Map.Entry::getKey, Map.Entry::getValue))); }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configurationAttributes) { OverrideAutoConfiguration annotation = AnnotatedElementUtils .findMergedAnnotation(testClass, OverrideAutoConfiguration.class); if (annotation != null && !annotation.enabled()) { return new DisableAutoConfigurationContextCustomizer(); } return null; }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configurationAttributes) { AnnotationsPropertySource propertySource = new AnnotationsPropertySource( testClass); return new PropertyMappingContextCustomizer(propertySource); }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configurationAttributes) { TypeExcludeFilters annotation = AnnotatedElementUtils .findMergedAnnotation(testClass, TypeExcludeFilters.class); if (annotation != null) { Set<Class<? extends TypeExcludeFilter>> filterClasses = new LinkedHashSet<Class<? extends TypeExcludeFilter>>( Arrays.asList(annotation.value())); return new TypeExcludeFiltersContextCustomizer(testClass, filterClasses); } return null; }
@Test public void getContextCustomizerWhenHasNoMappingShouldNotAddPropertySource() { ContextCustomizer customizer = this.factory .createContextCustomizer(NoMapping.class, null); ConfigurableApplicationContext context = mock( ConfigurableApplicationContext.class); ConfigurableEnvironment environment = mock(ConfigurableEnvironment.class); ConfigurableListableBeanFactory beanFactory = mock( ConfigurableListableBeanFactory.class); given(context.getEnvironment()).willReturn(environment); given(context.getBeanFactory()).willReturn(beanFactory); customizer.customizeContext(context, null); verifyZeroInteractions(environment); }
@Test public void hashCodeAndEqualsShouldBeBasedOnPropertyValues() throws Exception { ContextCustomizer customizer1 = this.factory .createContextCustomizer(TypeMapping.class, null); ContextCustomizer customizer2 = this.factory .createContextCustomizer(AttributeMapping.class, null); ContextCustomizer customizer3 = this.factory .createContextCustomizer(OtherMapping.class, null); assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode()); assertThat(customizer1).isEqualTo(customizer1).isEqualTo(customizer2) .isNotEqualTo(customizer3); }
@Test public void prepareContextShouldAddPropertySource() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(AttributeMapping.class, null); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); customizer.customizeContext(context, null); assertThat(context.getEnvironment().getProperty("mapped")).isEqualTo("Mapped"); }
@Test public void propertyMappingShouldNotBeUsedWithComponent() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(AttributeMapping.class, null); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(ConfigMapping.class); customizer.customizeContext(context, null); this.thrown.expect(BeanCreationException.class); this.thrown.expectMessage("The @PropertyMapping annotation " + "@PropertyMappingContextCustomizerFactoryTests.TypeMappingAnnotation " + "cannot be used in combination with the @Component annotation @Configuration"); context.refresh(); }
@Test public void getContextCustomizerWhenHasNoAnnotationShouldReturnNull() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(NoAnnotation.class, null); assertThat(customizer).isNull(); }
@Test public void getContextCustomizerWhenHasAnnotationEnabledTrueShouldReturnNull() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(WithAnnotationEnabledTrue.class, null); assertThat(customizer).isNull(); }
@Test public void getContextCustomizerWhenHasAnnotationEnabledFalseShouldReturnCustomizer() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(WithAnnotationEnabledFalse.class, null); assertThat(customizer).isNotNull(); }
@Test public void hashCodeAndEquals() throws Exception { ContextCustomizer customizer1 = this.factory .createContextCustomizer(WithAnnotationEnabledFalse.class, null); ContextCustomizer customizer2 = this.factory .createContextCustomizer(WithSameAnnotation.class, null); assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode()); assertThat(customizer1).isEqualTo(customizer1).isEqualTo(customizer2); }
@Test public void getContextCustomizerWhenHasAnnotationShouldReturnCustomizer() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(WithExcludeFilters.class, null); assertThat(customizer).isNotNull(); }
@Test public void hashCodeAndEquals() throws Exception { ContextCustomizer customizer1 = this.factory .createContextCustomizer(WithExcludeFilters.class, null); ContextCustomizer customizer2 = this.factory .createContextCustomizer(WithSameExcludeFilters.class, null); ContextCustomizer customizer3 = this.factory .createContextCustomizer(WithDifferentExcludeFilters.class, null); assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode()); assertThat(customizer1).isEqualTo(customizer1).isEqualTo(customizer2) .isNotEqualTo(customizer3); }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes) { // We gather the explicit mock definitions here since they form part of the // MergedContextConfiguration key. Different mocks need to have a different key. DefinitionsParser parser = new DefinitionsParser(); parser.parse(testClass); return new MockitoContextCustomizer(parser.getDefinitions()); }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes) { if (AnnotatedElementUtils.findMergedAnnotation(testClass, SpringBootTest.class) != null) { return new SpringBootTestContextCustomizer(); } return null; }
private List<ApplicationContextInitializer<?>> getInitializers( MergedContextConfiguration config, SpringApplication application) { List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>(); for (ContextCustomizer contextCustomizer : config.getContextCustomizers()) { initializers.add(new ContextCustomizerAdapter(contextCustomizer, config)); } initializers.addAll(application.getInitializers()); for (Class<? extends ApplicationContextInitializer<?>> initializerClass : config .getContextInitializerClasses()) { initializers.add(BeanUtils.instantiate(initializerClass)); } return initializers; }
@Override public ContextCustomizer createContextCustomizer(Class<?> testClass, List<ContextConfigurationAttributes> configAttributes) { if (AnnotatedElementUtils.findMergedAnnotation(testClass, Import.class) != null) { assertHasNoBeanMethods(testClass); return new ImportsContextCustomizer(testClass); } return null; }
@Test public void getContextCustomizerWithoutAnnotationReturnsCustomizer() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(NoMockBeanAnnotation.class, null); assertThat(customizer).isNotNull(); }
@Test public void contextCustomizerImportsBeans() throws Exception { ContextCustomizer customizer = this.factory .createContextCustomizer(TestWithImport.class, null); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); customizer.customizeContext(context, mock(MergedContextConfiguration.class)); context.refresh(); assertThat(context.getBean(ImportedBean.class)).isNotNull(); }