How to mock environment properties for unit testing ?

Antoine_Cordier
Star Contributor
Star Contributor

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.

1 ACCEPTED ANSWER

Damien_Metzler
Star Contributor
Star Contributor

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.

View answer in original post

2 REPLIES 2

Damien_Metzler
Star Contributor
Star Contributor

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.

It works, thank you.