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

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

最佳答案

不要忘记使用JUnitRunner:

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

08-16 03:56