我正在使用一个名为spring-cloud-aws的依赖模块。它具有@Configuration类,如org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration 在我的SpringBoot JUnit测试案例中,正在检测到SqsConfiguration类,并且正在初始化Bean。我想在我的JUNit测试案例的类中排除此配置。如何实现呢?
我尝试使用@ComponentScan无效。
Dictionary<Subreddit, int> data = new Dictionary<Subreddit, int>();
//Add some data
var sortedData = data.OrderBy(item => item, new MyDictionaryComparer());
加载此配置类要求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:请求中包含的安全令牌已过期
答案 0 :(得分:0)
有多种方法可以在测试期间排除特定的自动配置:
application-test.properties
中的属性排除spring.autoconfigure.exclude=org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration
@TestPropertySource
排除:@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = SQLTestConfig.class)
@TestPropertySource(properties ="spring.autoconfigure.exclude=org.springframework.cloud.aws.messaging.config.annotation.SqsConfiguration")
@EnableAutoConfiguration
排除,例如:@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = SQLTestConfig.class)
@EnableAutoConfiguration(exclude=SqsConfiguration.class)
选择一个更适合您的;)
答案 1 :(得分:0)
因此要禁用测试的所有 Bean 的自动加载,测试类可以明确提及所需的依赖项。这可以使用 ContextConfiguration
注释来完成。例如,
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {EmployeeService.class})
public class EmployeeLeavesTest {
@Autowired
private EmployeeService employeeService;
}
在这个例子中,只有 EmployeeService 类可用,其他 bean 不会被加载。