Using PowerMock to obtain the ATG Nucleus in testing results in NPE

╄→尐↘猪︶ㄣ 提交于 2019-12-13 03:24:55

问题


I am trying to test the following bit of code:

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;
    }
}

clearly Nucleus.getGlobalNucleus() is static.

According to the docs I should be able to use:

PowerMockito.mockStatic(Nucleus.class);
PowerMockito.when(Nucleus.getGlobalNucleus()).thenReturn(globalNucleusMock);    
PowerMockito.when(globalNucleusMock.resolveName("/atg/commerce/catalog/ProductCatalog");

Eitherway, I still get a nullpointer when I call:

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

回答1:


Don't forget to use JUnit runner :

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


来源:https://stackoverflow.com/questions/9373782/using-powermock-to-obtain-the-atg-nucleus-in-testing-results-in-npe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!