0

facing an issue when the code is trying to access the System.getProperty() in Spring Test cases. working when setting the VM argument on the test case that is running, but to run each test case class, have to set the VM argument explicitly.

Is there a way to set the environment variables in the Spring before the application context loads.

Thanks in advance

2
  • try putting System.setProperty(..) in a static block in test class Commented Jan 18, 2018 at 10:21
  • @pvpkiran, thanks it worked for me Commented Jan 18, 2018 at 11:27

2 Answers 2

2

Don't use System.getProperty().

Rather, use Spring's Enviornment abstraction with property sources (e.g., @PropertySource) and @Value("${propertyName}") to inject the value of the property into your production components (e.g., @Service classes, etc.).

Then, in your integration tests annotated with @RunWith(SpringRunner.class), use @TestPropertySource to override production property values with test property values.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this with the help of a static block, which is executed in the first place before anything else

static { System.setProperty("", ""); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.