Mercurial > hg > SpringPlayground
annotate spring-di-playground/src/test/java/de/comline/spring/ConfigurationTest.java @ 8:7ce4367b50bc
Ein echter Unit Test, der eine in-memory H2 als Teil vom Test startet, das Schema mit liquibase erzeugt und nach dem Test wieder stoppt. Das alles ohne die Web Controller zu starten.
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Thu, 13 Aug 2020 16:51:05 +0200 |
parents | 64e2ebad3366 |
children |
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.Bean; |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
13 import org.springframework.context.annotation.Configuration; |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
14 |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
15 import de.comline.spring.model.ActionMovieCatalog; |
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 ConfigurationTest { |
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("Explicit bean configuration") |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
22 public void beansInConfiguration() { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
23 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfiguration.class)) { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
24 MovieCatalog catalog = context.getBean(MovieCatalog.class); |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
25 assertThat(catalog.getCategory(), is("Comedy")); |
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("Duplicate beans can be registered") |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
31 public void duplicateBeansInConfiguration() { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
32 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DuplicateBeansConfiguration.class)) { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
33 assertThrows(NoUniqueBeanDefinitionException.class, () -> { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
34 context.getBean(MovieCatalog.class); |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
35 fail("Configuration 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
|
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 |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
40 @Configuration |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
41 private static class BeansConfiguration { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
42 BeansConfiguration() { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
43 super(); |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
44 } |
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 @Bean |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
47 MovieCatalog defineMovieCatalog() { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
48 return new ComedyMovieCatalog(); |
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 @Configuration |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
53 private static class DuplicateBeansConfiguration extends BeansConfiguration { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
54 @SuppressWarnings("unused") |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
55 DuplicateBeansConfiguration() { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
56 super(); |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
57 } |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
58 |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
59 @Bean |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
60 MovieCatalog duplicateMovieCatalog() { |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
61 return new ActionMovieCatalog(); |
64e2ebad3366
Import des plain Spring DI playground
Dirk Olmes <dirk.olmes@codedo.de>
parents:
diff
changeset
|
62 } |
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 } |