I am writing a set of junit test classes in Java.
There are several constants, for example strings that I will need in different test classes.
I am thinking about an interface that defines them and every test class would implement it.
The benefits I see there are:
- easy access to constants:
MY_CONSTANT
instead ofThatClass.MY_CONSTANT
- each constant defined only once
Is this approach rather a good or bad practice? I feel like doing so is a little like abusing the concept of interfaces.
You can answer generally about interfaces/constants, but also about unit tests if there is something special about it.
Best Answer
Joshua Bloch advises against this in his book titled Effective Java:
You can get the same effect with a normal class that defines the constants, and then use
import static com.example.Constants.*;