use*_*604 5 spring-boot spring-cloud-aws
我正在使用一个名为 spring-cloud-aws 的依赖模块。它有一个 @Configuration 类,如 org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration 在我的 SpringBoot JUnit 测试用例中,SqsConfiguration 类正在被检测到,并且 Bean 正在被初始化。我想在 JUNit 测试用例的类中排除此配置。如何实现这一目标?
我尝试使用@ComponentScan,但没有成功。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SQLTestConfig.class)
@ActiveProfiles("test")
public class BusinessManagerTest {
}
@TestConfiguration
@ComponentScan(basePackages = {"package1","package1"},
excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = SqsConfiguration.class)})
@Profile("test")
class SQLTestConfig {
@Bean
public SomeBean beans() {
return new SomeBean();
}
}
Run Code Online (Sandbox Code Playgroud)
加载此配置类需要可用的 aws 凭证。我不想注入凭据来运行简单的 Bean 测试用例。
org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]中定义的名为“simpleMessageListenerContainer”的bean时出错:调用init方法失败;嵌套异常是 com.amazonaws.services.sqs.model.AmazonSQSException:请求中包含的安全令牌已过期
有多种方法可以在测试期间排除特定的自动配置:
application-test.propertiesspring.autoconfigure.exclude=org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration
Run Code Online (Sandbox Code Playgroud)
@TestPropertySource:@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = SQLTestConfig.class)
@TestPropertySource(properties ="spring.autoconfigure.exclude=org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration")
Run Code Online (Sandbox Code Playgroud)
@EnableAutoConfiguration,例如:@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = SQLTestConfig.class)
@EnableAutoConfiguration(exclude=SqsConfiguration.class)
Run Code Online (Sandbox Code Playgroud)
选择一个更适合您的;)
| 归档时间: |
|
| 查看次数: |
6319 次 |
| 最近记录: |