5

I'm wondering if the @TestPropertySource will honor SpEL or will at least allow a property to substitute values from another property.

It's a similar question to @TestPropertySource with dynamic properties

Assuming the properties I'm referring to exist in one of the files in the locations attribute...

For example if I'd like to do something like:

@TestPropertySource(
    locations = {"classpath:application.properties", "classpath:database.properties"},
    properties = {"newPortNum = #{1 + Integer.parseInt(${myapp.web.server.port.ssl})}})

Or this:

@TestPropertySource(
    locations = {"classpath:application.properties", "classpath:database.properties"},
    properties = {"outputFile = ${outputDir}/foo.txt"})

Do I need to implement a TestExecutionListener or @BootstrapWith to accomplish this?

Community
  • 1
  • 1
Tony Falabella
  • 335
  • 1
  • 6
  • 17

1 Answers1

6

Straight from the Javadoc for @TestPropertySource.locations():

Property placeholders in paths (i.e., ${...}) will be resolved against the Environment.

... that means: against anything already added to the Environment.

SpEL expressions, on the other hand, are not supported.

If you need programmatic support for adding a PropertySource to the Environment, you should implement an ApplicationContextInitializer which can be registered via @ContextConfiguration(initializers = ...).

Regards,

Sam (author of the Spring TestContext Framework)

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136