0

We have a classic Maven, Spring (3.1.1) application where we created an applicationContext.xml.

In this file, we have declared a property placeholder with an external file and a file in classpath. Here an example found in another question here :

<context:property-placeholder location="file:${ADMIN_HOME}/db.properties,classpath:configuration.properties"
ignore-unresolvable="false" ignore-resource-not-found="false" />

It is working.

But now, we have a specific config file for JUnit tests. In this config file, we have imported the first one and added a property placeholder for tests with a classic declaration.

<import resource="applicationContext.xml" />

<context:property-placeholder location="classpath:configuration-test.properties"
    ignore-unresolvable="false" ignore-resource-not-found="false" />

We have injected a value from configuration-test.properties in JUnit test.

@Value("${junit.user.login}")
private String login;

But when we launch the JUnit, an error is raised. The key "junit.user.login" is not resolved.

We don't know why. Any idea ?

Thank you.

MychaL
  • 969
  • 4
  • 19
  • 37

1 Answers1

2

Is your junit launching the correct spring context? You added the correct xml paths to the test case like so?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/my-test-context.xml" })
public class TestCase{}
Nadir
  • 1,369
  • 1
  • 15
  • 28
  • Yes. @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/applicationContext-test.xml") public class DocumentServiceTest {} – MychaL Sep 10 '14 at 08:36
  • We didn't have a problem before to add the external file in main config file. All tests were working until we have this new feature. It is the fact to add "file:${ADMIN_HOME}/db.properties" which create a regression. – MychaL Sep 10 '14 at 08:38
  • In that case I think the issue is with the ${ADMIN_HOME} which is not known to spring when running a test. Maybe try to launch the test with additional -DADMIN_HOME= – Nadir Sep 10 '14 at 10:30
  • No pb with $ADMIN_HOME. All properties are loaded. The pb is for config test file. I succeed to execute my test if i set ignore-unresolvable = true in my applicationContext-test.xml. But i don't understand why, and if it is the good thing to do. – MychaL Sep 10 '14 at 11:34