簡體   English   中英

如何在單元測試中從“.properties”文件中讀取屬性?

[英]How do I read properties from ".properties" files in unit tests?

在產品代碼中,我可以使用以下代碼從application.propertiesapplication-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM