How to mock environment properties for unit testing ?

Hello,

I want to unit test a class where properties are accessed via the Framework.getProperties(“key”) method.

I don't know how to make this properties available in my class under test without having a running server.

I gess the answer is related to what is called features in the documentation but a dummy example of what has to be done would really help.

Thanks.

0 votes

1 answers

2581 views

ANSWER



Hello,

You can create your own Feature class by extending SimpleFeature (https://github.com/nuxeo/nuxeo/blob/master/nuxeo-runtime/nuxeo-runtime-test/src/main/java/org/nuxeo/runtime/test/runner/SimpleFeature.java)

In this class you have several hooks in which you can set your own properties.

After that, juste reference your feature class in the @Features() annotations.

To do some factorizations, you can also annotate your Feature class by @Features and @Deploy annotations, allowing you to define a ready to user Feature for your app. This will prevent you from using for instance @Features({CoreFeature.class, MyFeature.class}). Then all your test classes will just use @Features(MyFeature.class), minimizing the rework in case your setup changes.

0 votes



It works, thank you.
09/23/2015