我正在执行Junit测试的以下服务方法。 SUT类具有@Reference到ResourceResolverFactory服务。当我启动SUT类时,resourceResolverFactory始终为null,这会在try块中执行第一行时引发NullPointerException:
resourceResolver = resourceResolverFactory.getAdministrativeResourceResolver(null);
@Reference
ResourceResolverFactory resourceResolverFactory;
@Override
public PropertyStatusInfo getPropertyStatusInfo(String hotelCode,
String brandCode) {
LOGGER.info( "Class : " + this.getClass().getName()
+ " : Method : getPropertyStatusInfo() : [ENTER]");
LOGGER.debug( "Input Parameters : Hotel Code - "
+ hotelCode + " BrandCode - " + brandCode);
ResourceResolver resourceResolver;
Session session = null;
try {
resourceResolver = resourceResolverFactory.
getAdministrativeResourceResolver(null);
...
}
}
有什么建议?谢谢!
最佳答案
听起来好像工厂实例没有注入测试代码中。
您可以使用反射将resourceResolverFactory
字段设置为某个值value
:
Field field = SUT.class.getDeclaredField("resourceResolverFactory");
field.setAccessible(true);
field.set(instanceOfSutClass, value);
资料来源:https://stackoverflow.com/a/19976626/3895897