Olivier Grégoire
Olivier Grégoire

Reputation: 35417

Testing classes using System.getProperty(String)

I've written some utility class to use System.getProperty("user.home") and in it I added a specific path relative to that one based on System.getProperty("os.name").

What should I do to test my code in all the OS configurations I prepared and perform those tests in a single JUnit test class?

I was thinking about a SystemPropertyProvider which would act like Java 8's Clock or Guava's Stopwatch, but I'm not sure it's the best approach.

Upvotes: 0

Views: 1448

Answers (2)

Stefan Birkner
Stefan Birkner

Reputation: 24510

You can specify the system property and reset if afterwards. You can use the System Rules library for safely resetting the system property.

Upvotes: 1

artbristol
artbristol

Reputation: 32407

I would refactor the utility class to take a Properties via dependency injection. In the main code, give it System.getProperties(). In tests, give it your own Properties instance with test values.

(Be careful if one of the system properties could change - in that case, you'd need to retrieve a fresh Properties each time)

Upvotes: 4

Related Questions