comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:64e2ebad3366
1 package de.comline.spring;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.junit.jupiter.api.Assertions.assertThrows;
6 import static org.junit.jupiter.api.Assertions.fail;
7
8 import org.junit.jupiter.api.DisplayName;
9 import org.junit.jupiter.api.Test;
10 import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
11 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
12 import org.springframework.context.annotation.ComponentScan;
13 import org.springframework.context.annotation.ComponentScan.Filter;
14 import org.springframework.context.annotation.Configuration;
15 import org.springframework.context.annotation.FilterType;
16
17 import de.comline.spring.model.MovieCatalog;
18 import de.comline.spring.model.scanning.ActionMovieCatalogComponent;
19
20 public class ClasspathScanningTest {
21 @Test
22 @DisplayName("Scanning explicit packages")
23 public void classpathScanning() {
24 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("de.comline.spring.model.scanning")) {
25 assertThrows(NoUniqueBeanDefinitionException.class, () -> {
26 context.getBean(MovieCatalog.class);
27 fail("Classpath scanning must have detected both implementations of the MovieCatalog interface");
28 });
29 }
30 }
31
32 @Test
33 @DisplayName("Scanning by configuration")
34 public void explicitConfiguration() {
35 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScanningConfiguration.class)) {
36 assertThrows(NoUniqueBeanDefinitionException.class, () -> {
37 context.getBean(MovieCatalog.class);
38 fail("Classpath scanning must have detected both implementations of the MovieCatalog interface");
39 });
40 }
41 }
42
43 @Test
44 @DisplayName("Scanning by configuration with filter")
45 public void configurationWithFilter() {
46 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(FilteredScanningConfiguration.class)) {
47 MovieCatalog catalog = context.getBean(MovieCatalog.class);
48 assertThat(catalog.getCategory(), is("Comedy"));
49 }
50 }
51
52 /**
53 * Note: class may not be final.<br/>
54 * Note: class must have a default constructor which is package protected at
55 * least
56 */
57 @Configuration
58 @ComponentScan(basePackages = "de.comline.spring.model.scanning")
59 private static class ScanningConfiguration {
60 @SuppressWarnings("unused")
61 ScanningConfiguration() {
62 super();
63 }
64 }
65
66 @Configuration
67 @ComponentScan(basePackages = "de.comline.spring.model.scanning", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ActionMovieCatalogComponent.class))
68 private static class FilteredScanningConfiguration {
69 @SuppressWarnings("unused")
70 FilteredScanningConfiguration() {
71 super();
72 }
73 }
74 }