diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spring-di-playground/src/test/java/de/comline/spring/ConfigurationTest.java	Tue Aug 11 16:05:44 2020 +0200
@@ -0,0 +1,64 @@
+package de.comline.spring;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import de.comline.spring.model.ActionMovieCatalog;
+import de.comline.spring.model.ComedyMovieCatalog;
+import de.comline.spring.model.MovieCatalog;
+
+public class ConfigurationTest {
+	@Test
+	@DisplayName("Explicit bean configuration")
+	public void beansInConfiguration() {
+		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BeansConfiguration.class)) {
+			MovieCatalog catalog = context.getBean(MovieCatalog.class);
+			assertThat(catalog.getCategory(), is("Comedy"));
+		}
+	}
+	
+	@Test
+	@DisplayName("Duplicate beans can be registered")
+	public void duplicateBeansInConfiguration() {
+		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DuplicateBeansConfiguration.class)) {
+			assertThrows(NoUniqueBeanDefinitionException.class, () -> {
+				context.getBean(MovieCatalog.class);
+				fail("Configuration scanning must have detected both implementations of the MovieCatalog interface");
+			});
+		}
+	}
+	
+	@Configuration
+	private static class BeansConfiguration {
+		BeansConfiguration() {
+			super();
+		}
+		
+		@Bean
+		MovieCatalog defineMovieCatalog() {
+			return new ComedyMovieCatalog();
+		}
+	}
+	
+	@Configuration
+	private static class DuplicateBeansConfiguration extends BeansConfiguration {
+		@SuppressWarnings("unused")
+		DuplicateBeansConfiguration() {
+			super();
+		}
+		
+		@Bean
+		MovieCatalog duplicateMovieCatalog() {
+			return new ActionMovieCatalog();
+		}
+	}
+}