view spring-di-playground/src/test/java/de/comline/spring/ClasspathScanningTest.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.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

import de.comline.spring.model.MovieCatalog;
import de.comline.spring.model.scanning.ActionMovieCatalogComponent;

public class ClasspathScanningTest {
	@Test
	@DisplayName("Scanning explicit packages")
	public void classpathScanning() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("de.comline.spring.model.scanning")) {
			assertThrows(NoUniqueBeanDefinitionException.class, () -> {
				context.getBean(MovieCatalog.class);
				fail("Classpath scanning must have detected both implementations of the MovieCatalog interface");
			});
		}
	}

	@Test
	@DisplayName("Scanning by configuration")
	public void explicitConfiguration() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ScanningConfiguration.class)) {
			assertThrows(NoUniqueBeanDefinitionException.class, () -> {
				context.getBean(MovieCatalog.class);
				fail("Classpath scanning must have detected both implementations of the MovieCatalog interface");
			});
		}
	}

	@Test
	@DisplayName("Scanning by configuration with filter")
	public void configurationWithFilter() {
		try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(FilteredScanningConfiguration.class)) {
			MovieCatalog catalog = context.getBean(MovieCatalog.class);
			assertThat(catalog.getCategory(), is("Comedy"));
		}
	}

	/**
	 * Note: class may not be final.<br/>
	 * Note: class must have a default constructor which is package protected at
	 * least
	 */
	@Configuration
	@ComponentScan(basePackages = "de.comline.spring.model.scanning")
	private static class ScanningConfiguration {
		@SuppressWarnings("unused")
		ScanningConfiguration() {
			super();
		}
	}

	@Configuration
	@ComponentScan(basePackages = "de.comline.spring.model.scanning", excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = ActionMovieCatalogComponent.class))
	private static class FilteredScanningConfiguration {
		@SuppressWarnings("unused")
		FilteredScanningConfiguration() {
			super();
		}
	}
}