使用PowerMock在NPE测试结果中获取ATG Nucleus

时间:2012-02-21 07:34:05

标签: java mockito powermock atg

我正在尝试测试以下代码:

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");

无论如何,当我打电话时,我仍然得到一个nullpointer:

Nucleus.getGlobalNucleus().resolveName("/atg/commerce/catalog/ProductCatalog")

1 个答案:

答案 0 :(得分:2)

别忘了使用JUnit runner:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Nucleus.class)
public class YourClassTest {