In this post, I will present a convenient way to work with properties file inside unit tests.
This way requires properties, which is a project on Gitgub.
(To find out how get started and install properties, please read their official documentation from the link above).
You have a properties file that is used in production. That means that production code depends on values in this file.
You want to use the same values in your test.
- In your test, you add an annotation before the class declaration:
- You define a class member for each property that you want to use with the property annotaion
@Property("property_name_in_properties_file") private String your_property_variable;
- Add @Before method to your test class (if it doesn’t exist yet) and add following line:
Now, in your test, you can access each property from properties file using an internal class member.