annotate spring-di-playground/src/test/java/de/comline/spring/ClasspathScanningTest.java @ 5:227f3105fedd

Erster Unit Test, der das repository einfach nur mockt
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 13 Aug 2020 15:58:57 +0200
parents 64e2ebad3366
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
1 package de.comline.spring;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
2
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
3 import static org.hamcrest.MatcherAssert.assertThat;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
4 import static org.hamcrest.Matchers.is;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
5 import static org.junit.jupiter.api.Assertions.assertThrows;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
6 import static org.junit.jupiter.api.Assertions.fail;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
7
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
8 import org.junit.jupiter.api.DisplayName;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
9 import org.junit.jupiter.api.Test;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
10 import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
11 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
12 import org.springframework.context.annotation.ComponentScan;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
13 import org.springframework.context.annotation.ComponentScan.Filter;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
14 import org.springframework.context.annotation.Configuration;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
15 import org.springframework.context.annotation.FilterType;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
16
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
17 import de.comline.spring.model.MovieCatalog;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
18 import de.comline.spring.model.scanning.ActionMovieCatalogComponent;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
19
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
20 public class ClasspathScanningTest {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
21 @Test
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
22 @DisplayName("Scanning explicit packages")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
23 public void classpathScanning() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
24 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("de.comline.spring.model.scanning")) {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
25 assertThrows(NoUniqueBeanDefinitionException.class, () -> {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
26 context.getBean(MovieCatalog.class);
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
27 fail("Classpath scanning must have detected both implementations of the MovieCatalog interface");
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
28 });
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
29 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
30 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
31
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
32 @Test
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
33 @DisplayName("Scanning by configuration")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
34 public void explicitConfiguration() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
35 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScanningConfiguration.class)) {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
36 assertThrows(NoUniqueBeanDefinitionException.class, () -> {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
37 context.getBean(MovieCatalog.class);
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
38 fail("Classpath scanning must have detected both implementations of the MovieCatalog interface");
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
39 });
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
40 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
41 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
42
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
43 @Test
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
44 @DisplayName("Scanning by configuration with filter")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
45 public void configurationWithFilter() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
46 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(FilteredScanningConfiguration.class)) {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
47 MovieCatalog catalog = context.getBean(MovieCatalog.class);
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
48 assertThat(catalog.getCategory(), is("Comedy"));
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
49 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
50 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
51
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
52 /**
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
53 * Note: class may not be final.<br/>
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
54 * Note: class must have a default constructor which is package protected at
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
55 * least
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
56 */
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
57 @Configuration
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
58 @ComponentScan(basePackages = "de.comline.spring.model.scanning")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
59 private static class ScanningConfiguration {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
60 @SuppressWarnings("unused")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
61 ScanningConfiguration() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
62 super();
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
63 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
64 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
65
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
66 @Configuration
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
67 @ComponentScan(basePackages = "de.comline.spring.model.scanning", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ActionMovieCatalogComponent.class))
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
68 private static class FilteredScanningConfiguration {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
69 @SuppressWarnings("unused")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
70 FilteredScanningConfiguration() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
71 super();
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
72 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
73 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
74 }