Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行测试时排除SpringApplicationRunListener_Java_Spring_Spring Boot - Fatal编程技术网

Java 运行测试时排除SpringApplicationRunListener

Java 运行测试时排除SpringApplicationRunListener,java,spring,spring-boot,Java,Spring,Spring Boot,我使用了一个库来实现SpringApplicationRunListener类 public class FromLibApplicationRunListener implements SpringApplicationRunListener {...} 这个库在运行测试时加载了很多我不想要的东西。所以我尝试创建一个测试应用程序 @SpringBooApplication @ComponentScan(excudeFilters = @ComponentScan.Filters(type=

我使用了一个库来实现SpringApplicationRunListener类

public class FromLibApplicationRunListener implements SpringApplicationRunListener {...}
这个库在运行测试时加载了很多我不想要的东西。所以我尝试创建一个测试应用程序

@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

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

您可以使用配置文件:

  • 排除侦听器类的测试配置文件
  • @Profile(“!test”)
    libApplicationRunListener的公共类实现SpringApplicationRunListener{…}
    
  • 为测试类设置配置文件
  • 。。。
    @ActiveProfiles(“测试”)
    公共类MyTest{
    
    或在
    application.yaml
    文件(或
    application.properties
    )中设置应用程序活动配置文件


    另一种解决方案是在测试配置中重写bean:

    @SpringBootTest(classes=MyTest.TestConfig.class)
    类MyTest{
    @测试配置
    静态类TestConfig{
    @豆子
    public FromLibApplicationRunListener侦听器存根(){
    从libapplicationrunlistener()返回新的{
    //重写此处使用的SpringApplicationRunListener方法
    };
    }
    }
    ...
    }
    

    @SpringBootTest(classes=MyTest.TestConfig.class)
    类MyTest{
    @测试配置
    静态类TestConfig{
    @豆子
    公共SpringApplicationRunListener fromLibApplicationRunListener(){
    返回新的SpringApplicationRunListener(){};
    }
    }
    ...
    }
    
    我无法将代码添加到FromLibApplicationRunListener,因为它来自外部lib。在这种情况下,您可以在测试配置中覆盖
    FromLibApplicationRunListener
    bean。当我从Spring读取代码时,它将使用类加载器,从SpringApplicationRunListener加载所有类扩展。如果我从
    FromL创建类扩展iApplicationRunListener
    ,然后它将同时加载它们
    spring:
      profiles:
        active: dev