4

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 from src/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);
    }
}

structure

1 Answer 1

8

You can use @TestPropertySource annotation in your test class.

For example you have this attribute in your mailing.properties file:

[email protected]

Just annotate @TestPropertySource("classpath:config/mailing.properties") on your test class.

You should be able to read out the property for example with the @Value annotation.

@Value("${fromMail}")
private String fromMail;

To avoid annotate this annotation on multiple test classes you can implement a superclass or meta-annotations.


EDIT1:

@SpringBootApplication
@PropertySource("classpath:config/mailing.properties")
public class DemoApplication implements CommandLineRunner {

@Autowired
private MailService mailService;

public static void main(String[] args) throws Exception {
    SpringApplication.run(DemoApplication.class, args);
}

@Override
public void run(String... arg0) throws Exception {
    String s = mailService.getMailFrom();
    System.out.println(s);
}

MailService:

@Service
public class MailService {

    @Value("${mailFrom}")
    private String mailFrom;

    public String getMailFrom() {
        return mailFrom;
    }

    public void setMailFrom(String mailFrom) {
        this.mailFrom = mailFrom;
    }
}

DemoTestFile:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = DemoApplication.class)
@TestPropertySource("classpath:config/mailing.properties")
public class DemoApplicationTests {

    @Autowired
    MailService mailService;

    @Test
    public void contextLoads() {
        String s = mailService.getMailFrom();
        System.out.println(s);
    }
}

enter image description here

8
  • but file .properties can stay in src/main/resources... no need to make src/test/resources...?
    – Bublik
    Oct 3, 2016 at 7:24
  • yes, correct. I suppose you have your config like this src/main/resources/config/mailing.properties. If yes it should work
    – Patrick
    Oct 3, 2016 at 7:26
  • still get: Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/config/mailing.properties] also added simple sources, on which i tested your sollution
    – Bublik
    Oct 3, 2016 at 7:39
  • @Bublik just tried a new project build on your code and it works. Can you show your project structure. updated my answer with my test code
    – Patrick
    Oct 3, 2016 at 8:01
  • just added structure
    – Bublik
    Oct 3, 2016 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.