Spring boot @DataJpaTest 排除过滤器不起作用 [英] Spring boot @DataJpaTest exclude filter doesn't work

查看:50
本文介绍了Spring boot @DataJpaTest 排除过滤器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个测试:

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

不应扫描此包中的存储库:io.rainrobot.adwisdom.repository.ex.* -但是当我运行测试时,我收到此错误:

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!

这里是配置:

    @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;
        }
    }

和 application.properties:

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

如何使用 @DataJpaTest 排除组件?

how can i exclude components with @DataJpaTest ?

推荐答案

过滤器只影响组件扫描.如果扫描发现您的配置这一行:

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.

这篇关于Spring boot @DataJpaTest 排除过滤器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆