如何解决Spring Boot 测试 - 使用 test application.yaml 时 @Value 为空
我正在尝试运行带有测试值的单元测试 application.yaml 文件,但我的 @Value 变量中仍然收到 null 值。
@RunWith(springrunner.class)
@SpringBoottest
@ContextConfiguration(classes = { SpringBatchConfiguration.class })
public class WebServiceReaderTest {
//some mock services here
@InjectMocks
private CustomWebServiceReader reader;
private JobParameters jobParameters;
@Test
public void readertest() throws Exception {
StepExecution stepExecution = MetaDataInstanceFactory
.createStepExecution(jobParameters);
StepScopeTestUtils.doInStepScope(stepExecution,() -> reader.read());
}
}
SpringBatchConfiguration.class
@Configuration
@EnableAutoConfiguration
@EnableBatchProcessing
public class SpringBatchConfiguration extends DefaultBatchConfigurer {
@Override
public void setDataSource(DataSource dataSource) {
// leave empty,so the spring batch will not use database,but a Map
}
}
@Value("${data-import.threshold}")
private Long threshold;
我在 src/test/resources 文件夹中有我的 application.yaml 文件
data-import:
threshold: 10
我使用的是 Spring Boot 2.4.4 和 wiremock 2.27.2。
我试过使用
@ContextConfiguration(classes = { SpringBatchConfiguration.class },initializers=
ConfigDataApplicationContextinitializer.class)
@ContextConfiguration(classes = { SpringBatchConfiguration.class },initializers=
ConfigFileApplicationContextinitializer.class)
@TestPropertySource(locations = { "classpath:application.yaml" })
但这些都没有用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。