我使用了一个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
运行测试时如何删除该库?
最佳答案
您可以为此使用配置文件:
- 排除监听器类的测试配置文件
@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/