Skip to Content
Author's profile photo Former Member

A Convenient way to work with properties in junit

What
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).


Motivation
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.

How

 

  1. In your test, you add an annotation before the class declaration:
    @Resource.File("path/To/preperties/file.properties")
  2.  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;
  3. Add @Before method to your test class (if it doesn’t exist yet) and add following line:
    PropertyLoader.newInstance().populate(this);

Now, in your test, you can access each property from properties file using an internal class member.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.