我正在尝试测试以下代码:
GSARepository productCatalog = (GSARepository) Nucleus.getGlobalNucleus().resolveName("/atg/commerce/catalog/ProductCatalog");
for (RepositoryItem orderItem : orderItems) {
String product = (String) orderItem.getPropertyValue(PropertyNameConstants.PRODUCTID);
if (!ProductUtils.isSpecial(product, productCatalog)) {
isSpecial = false;
break;
}
}
显然Nucleus.getGlobalNucleus()是静态的。
根据文档,我应该能够使用:
PowerMockito.mockStatic(Nucleus.class);
PowerMockito.when(Nucleus.getGlobalNucleus()).thenReturn(globalNucleusMock);
PowerMockito.when(globalNucleusMock.resolveName("/atg/commerce/catalog/ProductCatalog");
这样,当我调用时,我仍然会得到一个空指针
Nucleus.getGlobalNucleus().resolveName("/atg/commerce/catalog/ProductCatalog")
不要忘记使用JUnit runner:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Nucleus.class)
public class YourClassTest {