Mercurial > hg > SpringPlayground
view spring-di-playground/src/test/java/de/comline/spring/QualifierTest.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 source
package de.comline.spring; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.NoUniqueBeanDefinitionException; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import de.comline.spring.model.ActionMovieCatalog; import de.comline.spring.model.ActionMovieLister; import de.comline.spring.model.ComedyMovieCatalog; import de.comline.spring.model.MovieCatalog; public class QualifierTest { @Test @DisplayName("Requesting explicit interface type") public void explicitType() { try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ActionMovieCatalog.class, ComedyMovieCatalog.class)) { MovieCatalog action = context.getBean(ActionMovieCatalog.class); assertThat(action.getCategory(), is("Action")); } } @Test @DisplayName("Qualifier can be specified at source") public void qualifierAtSource() { // Note how we add multiple interface implementations to the context try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ActionMovieCatalog.class, ComedyMovieCatalog.class, ActionMovieLister.class)) { ActionMovieLister lister = context.getBean(ActionMovieLister.class); assertThat(lister.toString(), containsString("Action")); } } @Test @DisplayName("Binding only a single interface implementation") public void singleImplementationOfInterface() { try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ComedyMovieCatalog.class)) { MovieCatalog catalog = context.getBean(MovieCatalog.class); assertThat(catalog.getCategory(), is("Comedy")); } } @Test @DisplayName("Multiple implementations of the same interface cannot be injected") public void multipleInterfaceImplementations() { try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ActionMovieCatalog.class, ComedyMovieCatalog.class)) { assertThrows(NoUniqueBeanDefinitionException.class, () -> { context.getBean(MovieCatalog.class); }); } } }