java - 运行测试时排除 SpringApplicationRunListener

标签 java spring spring-boot

我使用了一个lib,其中有一个类实现SpringApplicationRunListener

public class FromLibApplicationRunListener implements SpringApplicationRunListener {...}

这个库加载了很多我在运行测试时不想拥有的东西。所以我尝试创建一个 TestApplication

@SpringBooApplication
@ComponentScan(excudeFilters = @ComponentScan.Filters(type= ASSIGNABLE_TYPE, 
   value={RealApp.java, FromLibApplicatioRunListener})
public class TestApp {
   public static main(String[] args) {
     SpringApplication.run(TestApp.class ,args);
   }
}

但是当我运行TestApp时,它仍然抛出异常

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.....FromLibApplicationRunListner]: Constructor threw exception; nested exception is java.lang.ExceptionInInitializerError
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184)
    at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:435)
    ... 6 more

运行测试时如何删除该库?

最佳答案

您可以为此使用配置文件:

  1. 排除监听器类的测试配置文件
@Profile("!test")
public class FromLibApplicationRunListener implements SpringApplicationRunListener {...}
  • 为测试类设置配置文件
  • ...
    @ActiveProfiles("test")
    public class MyTest {
    

    或在 application.yaml 文件(或 application.properties)中设置应用程序 Activity 配置文件

    spring:
      profiles:
        active: dev
    

    关于java - 运行测试时排除 SpringApplicationRunListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60456981/

    相关文章:

    java - 如何使用 Spring Boot 2.1.1 从外部文件夹获取 "display"文件

    java - 从 json 文本文件加载 JSONObject 的最佳方法是什么?

    java - 复合对象中的父子关系?

    java - 基于 Spring MVC 注解的 Hello World 不起作用

    java - Spring:注释配置基础包

    java - 在 Spring Boot 2 中验证 Oauth2 JWT token

    java - 如何让嵌入式mongodb在应用程序关闭时保留数据?

    java - Java 中的 StringIndexOutOfBoundsException?

    java - 如何使用AWS SQS和SNS向网站发送通知消息?

    java - 调用 Spring 服务并通过构造函数发送参数