【发布时间】:2016-10-03 06:18:49
【问题描述】:
我有简单的 Spring Boot Web 服务,我使用 .properties 文件进行配置。作为 spring-mail 配置的示例,我在 src/main/resources/config/ 文件夹中有单独的文件 mailing.properties。
在主应用程序中我使用:
@PropertySource(value = { "config/mailing.properties" })
问题出现在测试时,我想使用这个文件中的相同属性,但是当我尝试使用它时,我得到fileNotFaundExeption。
问题是:
- 我应该在我的
src/test文件夹中有单独的资源,还是可以从src/main文件夹中访问资源,如果是,如何?
更新添加的来源
测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:config/mailing.properties")
public class DemoApplicationTests {
@Autowired
private TestService testService;
@Test
public void contextLoads() {
testService.printing();
}
}
服务等级:
@Service
public class TestService
{
@Value("${str.pt}")
private int pt;
public void printing()
{
System.out.println(pt);
}
}
主应用类:
@SpringBootApplication
@PropertySource(value = { "config/mailing.properties" })
public class DemoApplication {
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}
【问题讨论】:
标签: java spring testing spring-boot