Mercurial > hg > SpringPlayground
view spring-di-playground/src/test/java/de/comline/spring/ConfigurationTest.java @ 20:452cfefb0e1e default tip
Einfache Spring Application, die ihre config aus dem Spring Cloud Config Server liest
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Thu, 17 Sep 2020 17:25:15 +0200 |
parents | 64e2ebad3366 |
children |
line wrap: on
line source
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(); } } }