0

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

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

1 回答 1

2

不要忘记使用 JUnit runner:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Nucleus.class)
public class YourClassTest {
于 2012-02-21T07:42:13.107 回答