Spring boot @DataJpaTest exclude filter doesn't work

有些话、适合烂在心里 提交于 2020-08-08 05:24:47

问题


I Have this test:

    @RunWith(SpringRunner.class)
    @DataJpaTest(excludeFilters = @Filter(type = FilterType.REGEX,
                                    pattern = "io\\.rainrobot\\.adwisdom\\.repository\\.es\\..*"))
    public class AskTest {

that should not scan repositories in this packege: io.rainrobot.adwisdom.repository.ex.* - but when i run the test i get this error:

Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.domain.Page io.rainrobot.adwisdom.repository.es.AskElasticRepository.findByTitle(java.lang.String,org.springframework.data.domain.PageRequest)! No property title found for type Ask!

here are the configurations:

    @SpringBootApplication public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }}
    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(basePackages="io.rainrobot.adwisdom.repository.sql") 
    @EntityScan(basePackages = {"io.rainrobot.adwisdom.dto"},
                basePackageClasses = {Jsr310JpaConverters.class})
    public class  JpaConfig {
    }
    @Configuration
    @EnableElasticsearchRepositories(basePackages=
        "io.rainrobot.adwisdom.repository.es")
    @EntityScan(basePackages = {"io.rainrobot.adwisdom.dto"},
        basePackageClasses = {Jsr310JpaConverters.class})
    public class ElasticsearchCongifuration {
        @Value("${elasticsearch.host:localhost}")
        public String host;
        @Value("${elasticsearch.port:9300}")
        public int port;

        @Bean
        public Client client(){
            TransportClient client = null;
            try{
                InetAddress inetAddress = InetAddress.getByName(host);
                TransportAddress transportAddress
                    = new TransportAddress(inetAddress, port);
                client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(transportAddress);
             } catch (UnknownHostException e) {  }
             return client;
        }
    }

and the application.properties:

# JPA config
spring.datasource.url=jdbc:mysql://localhost:3306/advisdom?useTimezone=true&serverTimezone=UTC&createDatabaseIfNotExist=true
spring.datasource.username=admin
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

# Local Elasticsearch config
elasticsearch.host=localhost
elasticsearch.port=9200

loggin.level.org.springframework: DEBUG

# App config
server.port=8102
spring.application.name=BootElastic

how can i exclude components with @DataJpaTest ?


回答1:


The filter only affects the component scan. If that scan finds your configuration this line:

@EnableElasticsearchRepositories(basePackages=
    "io.rainrobot.adwisdom.repository.es")

Will then enable the repositories for the package you tried to exclude.

At least that is my understanding. It should be easy to verify if you remove that line from you configuration.



来源:https://stackoverflow.com/questions/58243114/spring-boot-datajpatest-exclude-filter-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!