Mercurial > hg > SpringPlayground
view spring-di-playground/src/test/java/de/comline/spring/QualifierTest.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.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); }); } } }