how to get org.apache.felix.scr.annotations.Refere

2019-08-05 04:40发布

问题:

In my unit class I have following line of code

@Reference private ConfigService configService;

and this is being used in this class at many places, but when it comes to testing I dont have any way to set this for testing purpose as there are no getters and setters for this property. Please let me know how can I get this using Mockito

回答1:

Private field value can be set with reflection without Mockito:

    Field field = YoursClass.class.getDeclaredField("configService");
    field.setAccessible(true);
    field.set(YoursClassInstance, value);