在Spring中使用@TestPropertySource时发生绑定异常

问题描述 投票:0回答:1

我正在尝试加载 application-test.yaml 文件进行测试

这是我的代码,

@Provider("provider")
@PactBroker
//@ActiveProfiles({"test"})
@TestPropertySource("classpath:application-test.yaml")
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 

当我使用@TestPropertySource注释时,出现异常。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:  Could not bind properties to 'Myclass' : prefix=spring.security.verification, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.security.verification' to com.test.Myclass

但是,如果我使用 @ActiveProfiles({"test"}) 代替,一切都会按预期工作。

我想知道为什么行为会有差异。我假设这两个注释会达到相同的结果。

spring spring-boot spring-test
1个回答
0
投票

尝试使用 @TestPropertySource("classpath:/application-test.yaml")

© www.soinside.com 2019 - 2024. All rights reserved.