Using PowerMock to obtain the ATG Nucleus in testing results in NPE
Question
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")
Solution
Don't forget to use JUnit runner :
@RunWith(PowerMockRunner.class)
@PrepareForTest(Nucleus.class)
public class YourClassTest {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow