2

Test configuration for loading messages.proeprties is

<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="classpath:i18n/messages,classpath:i18n/application" p:fallbackToSystemLocale="false"/>

If I run a single test on Intellij (Junit test runner), it won't be able to load messages.properties from src/main/webapp/WEB-INF/i18n/messages.properties.

Running the same test from console (mvn -Dtest=blablatest test) works.

In order to make it work on Intellij, I have to copy the messages.properties to src/test/resources/i18n/messages.properties.

Update1: "Running the same test from console (mvn -Dtest=blablatest test) works."
Works only if the project is built first.

Chris
  • 549
  • 1
  • 5
  • 18
  • `src/main/webapp` isn't the class path. You should put them in `src/main/resources` instead for `classpath:` to pick them up. – M. Deinum Jul 02 '15 at 11:54
  • if the purpose of your test is testing messages.properties itself (unlikely) you can follow the M.Deinum suggestion. If the purpose of your test is testing *something* that needs a messages.properties I would not rather add src/main/resources as classpath of my tests but I'd have a different set of *.properties only for test purposes instead. – m c Jul 02 '15 at 11:59
  • @medveshonok117 The purpose of the testing is something that needs a `messages.properties`. If I put it in `src/main/resources` it's ok for the unit test but the thing is that by default the messages properties for the project is `src/main/webapp/WEB-INF/i18n/messages.properties`. – Chris Jul 02 '15 at 12:06
  • OK, so my point is that you *do not* want your tests to load messages.properties from the main classpath: the correct approach is leaving completely separated your "live" and unit test environments. – m c Jul 02 '15 at 14:09
  • @medveshonok117 I don't need different messages for testing purposes, why should I use a separate file in that case, placed in test resources and maintain two files? Why is that the correct approach? – Chris Jul 02 '15 at 14:29
  • Unit tests *usually* do not require to run against *production* configuration - this is the job of integration tests - but using mocked / stubbed configuration / data instead. Giving the test environment the visibility to production (or any other non-test) configuration may lead to further problems (e.g. database / jms unwanted connections to wrong servers). http://stackoverflow.com/questions/5357601/whats-the-difference-between-unit-tests-and-integration-tests If you find "correct approach" too much restrictive you can see this as "best practice". – m c Jul 02 '15 at 15:26
  • agree, just was hopping you'd have some interesting reference. Thanks. – Chris Jul 02 '15 at 15:54

1 Answers1

1

You could add src/main/webapp/WEB-INF/i18n to test resources: File > Project Structure ... > Project Settings > Modules

Stefan Birkner
  • 24,059
  • 12
  • 57
  • 72
  • thanks. It works for Intellij this way but I realised (see update) that it's not working properly when I run the test form maven, e.g. `mvn clean;` `mvn -Dtest=blablatest test` – Chris Jul 02 '15 at 14:31