Mercurial > hg > SpringPlayground
view spring-di-playground/src/test/java/de/comline/spring/ConfigurationTest.java @ 13:a633d0623278
Die Sequenz startet bei 1 und wird auch nur um 1 erhoeht
author | Dirk Olmes <dirk.olmes@codedo.de> |
---|---|
date | Fri, 14 Aug 2020 08:13:57 +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(); } } }