我有一个Spring Boot应用程序,我通过扩展InfoContributor来扩展/ monitoring / info端点:
@Component
public class MyInfoContributor implements InfoContributor {
@Value("${filename}")
private String filename;
@Override
public void contribute(Builder builder) {
...
}
}
贡献者读取一个文件,该文件的名称是"文件名"变量并用它的内容填充响应。
然后我为它实现了一个测试,在那里我将通常的属性值替换为特定于此测试的属性值:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(properties = {
"filename=/test/some-file"
})
public class MyInfoContributorTest {
@Autowired
protected MockMvc mvc;
@Test
public void shouldReturnBuildInfo() throws Exception {
mvc.perform(get("/monitoring/info"))
.andExpect(content().string("..."))
.andExpect(status().isOk());
}
}
测试有效,但有一个问题:它正在寻找这个" / test / some-file" main / resources中的文件(在TestPropertySource中指定)而不是test / resources。 我也尝试了使用测试配置文件的方法,结果是一样的。
是否可以让此组件在测试资源中查找文件?
答案 0 :(得分:0)
试试这个:
@TestPropertySource(
locations = "classpath:test.properties",
properties = {"filename=/test/some-file"} )
放置在test.properties
中的src/test/resources
。
请注意,位置可以是字符串数组,条目顺序定义优先级,properties
具有在locations
中找到的文件中定义的最高优先级覆盖属性。