annotate spring-di-playground/src/test/java/de/comline/spring/QualifierTest.java @ 7:0c3494137a82

Starte eine H2 web console auf port 8081 fuer besseres DB debugging.
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 13 Aug 2020 16:28:20 +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.containsString;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
5 import static org.hamcrest.Matchers.is;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
6 import static org.junit.jupiter.api.Assertions.assertThrows;
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.ConfigurableApplicationContext;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
12 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
13
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
14 import de.comline.spring.model.ActionMovieCatalog;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
15 import de.comline.spring.model.ActionMovieLister;
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
16 import de.comline.spring.model.ComedyMovieCatalog;
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
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
19 public class QualifierTest {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
20 @Test
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
21 @DisplayName("Requesting explicit interface type")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
22 public void explicitType() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
23 try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ActionMovieCatalog.class, ComedyMovieCatalog.class)) {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
24 MovieCatalog action = context.getBean(ActionMovieCatalog.class);
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
25 assertThat(action.getCategory(), is("Action"));
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
26 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
27 }
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 @Test
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
30 @DisplayName("Qualifier can be specified at source")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
31 public void qualifierAtSource() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
32 // Note how we add multiple interface implementations to the context
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
33 try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ActionMovieCatalog.class, ComedyMovieCatalog.class, ActionMovieLister.class)) {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
34 ActionMovieLister lister = context.getBean(ActionMovieLister.class);
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
35 assertThat(lister.toString(), containsString("Action"));
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
36 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
37 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
38
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
39 @Test
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
40 @DisplayName("Binding only a single interface implementation")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
41 public void singleImplementationOfInterface() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
42 try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ComedyMovieCatalog.class)) {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
43 MovieCatalog catalog = context.getBean(MovieCatalog.class);
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
44 assertThat(catalog.getCategory(), is("Comedy"));
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
45 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
46 }
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
47
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
48 @Test
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
49 @DisplayName("Multiple implementations of the same interface cannot be injected")
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
50 public void multipleInterfaceImplementations() {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
51 try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ActionMovieCatalog.class, ComedyMovieCatalog.class)) {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
52 assertThrows(NoUniqueBeanDefinitionException.class, () -> {
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
53 context.getBean(MovieCatalog.class);
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
54 });
64e2ebad3366 Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff changeset
55 }
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 }