lundi 17 février 2020

Scan for specific entity in Spring test slice

I am creating an integration test with @WebMvcTest which imports the following configuration:

    @ComponentScan(
            basePackageClasses = {
                    DummyComponent.class
            },
            useDefaultFilters = false,
            includeFilters = {
                    @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = DummyComponent.class)
            }
    )

    @Configuration
    @EnableJpaRepositories(
            basePackageClasses = {
                    DummyRepository.class
            },
            includeFilters = {
                    @ComponentScan.Filter(type = ASSIGNABLE_TYPE, value = DummyRepository.class)
            }
    )
    public class DummyWebMvcConfig {

        private static String[] MODEL_PACKAGES = {
                "my.dummy.entity.package"
        };

        @Bean
        public DataSource getDataSource() {
            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
            return builder.setType(EmbeddedDatabaseType.H2).build();
        }

        @Bean
        public EntityManagerFactory entityManagerFactory() {
            HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            vendorAdapter.setGenerateDdl(true);
            vendorAdapter.setShowSql(true);

            LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
            factory.setJpaVendorAdapter(vendorAdapter);
            factory.setPackagesToScan(MODEL_PACKAGES);

            factory.setDataSource(getDataSource());
            factory.afterPropertiesSet();

            return factory.getObject();
        }

        @Bean
        public PlatformTransactionManager transactionManager() {
            JpaTransactionManager txManager = new JpaTransactionManager();
            txManager.setEntityManagerFactory(entityManagerFactory());
            return txManager;
        }
    }

When it comes to components and repositories I am able to filter all unnecessary ones using basePackageClasses and includeFilters for both @ComponentScan and @EnableJpaRepositories, thereby loading only DummyComponent, DummyRepository.

Unfortunately, I am loading every entity present in my.dummy.entity.package and I have no idea how to load exclusively DummyEntity. Is it possible (and if so how) to select only one entity from a package?

Thank you for your help.

Aucun commentaire:

Enregistrer un commentaire