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

Bublik
  • 912
  • 5
  • 15
  • 30

1 Answers1

8

You can use @TestPropertySource annotation in your test class.

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

mailFrom=fromMe@mail.com

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

Patrick
  • 12,336
  • 15
  • 73
  • 115
  • but file `.properties` can stay in `src/main/resources...` no need to make `src/test/resources...`? – Bublik Oct 03 '16 at 07:24
  • yes, correct. I suppose you have your config like this `src/main/resources/config/mailing.properties`. If yes it should work – Patrick Oct 03 '16 at 07: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 03 '16 at 07: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 03 '16 at 08:01
  • just added structure – Bublik Oct 03 '16 at 08:41
  • why is your application.properties file under `/config` and not under `/resources`? Spring-boot standardly searches for application.properties in `/src/main/resoureces/`. The mailing.properties can be inside config. – Patrick Oct 03 '16 at 08:44
  • it doesn't matter, problem is with `mailing.properties`. Moreover, `http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html` point 24.3. – Bublik Oct 03 '16 at 09:06
  • I find the problem: I do not know Why but if I use `@SpringApplicationConfiguration(classes = DemoApplication.class)` which is deprecated, in case of @SpringBootTest everything works. – Bublik Oct 03 '16 at 09:14