I have simple spring boot web service, where for configuration I use .properties
files. As example for spring-mail configuration, I have separate file mailing.properties
located in src/main/resources/config/
folder.
in main application I include it using:
@PropertySource(value = { "config/mailing.properties" })
The problem appears when it comes to tests, I would like to use the same properties from this file, but when i try to use it, I get fileNotFaundExeption
.
Question is:
- Should I have separate resources in my
src/test
folder, or it is possible to access resources fromsrc/main
folder, if yes, how?
UPDATE added sources
test class:
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource("classpath:config/mailing.properties")
public class DemoApplicationTests {
@Autowired
private TestService testService;
@Test
public void contextLoads() {
testService.printing();
}
}
service class:
@Service
public class TestService
{
@Value("${str.pt}")
private int pt;
public void printing()
{
System.out.println(pt);
}
}
main app class:
@SpringBootApplication
@PropertySource(value = { "config/mailing.properties" })
public class DemoApplication {
public static void main(String[] args)
{
SpringApplication.run(DemoApplication.class, args);
}
}