Mercurial > hg > SpringPlayground
diff spring-di-playground/src/test/java/de/comline/spring/ClasspathScanningTest.java @ 0:64e2ebad3366
Import des plain Spring DI playground
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Tue, 11 Aug 2020 16:05:44 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spring-di-playground/src/test/java/de/comline/spring/ClasspathScanningTest.java Tue Aug 11 16:05:44 2020 +0200 @@ -0,0 +1,74 @@ +package de.comline.spring; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.NoUniqueBeanDefinitionException; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.FilterType; + +import de.comline.spring.model.MovieCatalog; +import de.comline.spring.model.scanning.ActionMovieCatalogComponent; + +public class ClasspathScanningTest { + @Test + @DisplayName("Scanning explicit packages") + public void classpathScanning() { + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("de.comline.spring.model.scanning")) { + assertThrows(NoUniqueBeanDefinitionException.class, () -> { + context.getBean(MovieCatalog.class); + fail("Classpath scanning must have detected both implementations of the MovieCatalog interface"); + }); + } + } + + @Test + @DisplayName("Scanning by configuration") + public void explicitConfiguration() { + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScanningConfiguration.class)) { + assertThrows(NoUniqueBeanDefinitionException.class, () -> { + context.getBean(MovieCatalog.class); + fail("Classpath scanning must have detected both implementations of the MovieCatalog interface"); + }); + } + } + + @Test + @DisplayName("Scanning by configuration with filter") + public void configurationWithFilter() { + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(FilteredScanningConfiguration.class)) { + MovieCatalog catalog = context.getBean(MovieCatalog.class); + assertThat(catalog.getCategory(), is("Comedy")); + } + } + + /** + * Note: class may not be final.<br/> + * Note: class must have a default constructor which is package protected at + * least + */ + @Configuration + @ComponentScan(basePackages = "de.comline.spring.model.scanning") + private static class ScanningConfiguration { + @SuppressWarnings("unused") + ScanningConfiguration() { + super(); + } + } + + @Configuration + @ComponentScan(basePackages = "de.comline.spring.model.scanning", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ActionMovieCatalogComponent.class)) + private static class FilteredScanningConfiguration { + @SuppressWarnings("unused") + FilteredScanningConfiguration() { + super(); + } + } +}