简体   繁体   English

Spring 启动 junit 测试的自定义属性

[英]Spring boot custom properties for junit test

Here is the class that gets its pathToFile from application.properties.这是从 application.properties 获取其 pathToFile 的 class。

@Component
public class CSVReader {

    @Value("${name.basics}")
    private String pathToFile;

   (other code)
}

The problem is: how can I substitute application.properties for testing?问题是:如何用 application.properties 代替测试?

You can use spring boot test annotation:您可以使用 spring 启动测试注释:

@RunWith(SpringRunner.class)
@SpringBootTest(properties = {"name.basics=whatever"})
public class YourTestClass {

@Autowired 
CSVReader cvsReader;

@Test
public void yourTest() {
//...

Second option is to put application-test.properties containing replacements in src/test/resources folder of your project第二种选择是将包含替换的application-test.properties放在项目的src/test/resources文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM