[英]How do I read properties from ".properties" files in unit tests?
在產品代碼中,我可以使用以下代碼從application.properties
和application-development.properties
讀取aws.default.region
屬性
@Value("${aws.default.region:null}")
private String awsDefaultRegion;
但是,在單元測試中,相同的代碼不起作用,並且awsDefaultRegion
變量始終為 null。
我嘗試添加以下注釋,但它不起作用。
@PropertySource({
"classpath:application.properties",
"classpath:application-development.properties"
})
如果您使用 JUnit:
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:test-application.properties")
我認為在測試中注入價值可能是另一種好方法
ReflectionTestUtils.setField(
serviceMocked,
"awsDefaultRegion", // name attr in service
valueaTobeInjectWhenTesting
);
// rest of test method
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.