comparison spring-di-playground/src/test/java/de/comline/spring/ConfigurationTest.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.Bean;
13 import org.springframework.context.annotation.Configuration;
14
15 import de.comline.spring.model.ActionMovieCatalog;
16 import de.comline.spring.model.ComedyMovieCatalog;
17 import de.comline.spring.model.MovieCatalog;
18
19 public class ConfigurationTest {
20 @Test
21 @DisplayName("Explicit bean configuration")
22 public void beansInConfiguration() {
23 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfiguration.class)) {
24 MovieCatalog catalog = context.getBean(MovieCatalog.class);
25 assertThat(catalog.getCategory(), is("Comedy"));
26 }
27 }
28
29 @Test
30 @DisplayName("Duplicate beans can be registered")
31 public void duplicateBeansInConfiguration() {
32 try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DuplicateBeansConfiguration.class)) {
33 assertThrows(NoUniqueBeanDefinitionException.class, () -> {
34 context.getBean(MovieCatalog.class);
35 fail("Configuration scanning must have detected both implementations of the MovieCatalog interface");
36 });
37 }
38 }
39
40 @Configuration
41 private static class BeansConfiguration {
42 BeansConfiguration() {
43 super();
44 }
45
46 @Bean
47 MovieCatalog defineMovieCatalog() {
48 return new ComedyMovieCatalog();
49 }
50 }
51
52 @Configuration
53 private static class DuplicateBeansConfiguration extends BeansConfiguration {
54 @SuppressWarnings("unused")
55 DuplicateBeansConfiguration() {
56 super();
57 }
58
59 @Bean
60 MovieCatalog duplicateMovieCatalog() {
61 return new ActionMovieCatalog();
62 }
63 }
64 }