问题
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