0

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")
radimpe
  • 3,197
  • 2
  • 27
  • 46

1 Answers1

2

Don't forget to use JUnit runner :

@RunWith(PowerMockRunner.class)
@PrepareForTest(Nucleus.class)
public class YourClassTest {
Stanislav Levental
  • 2,165
  • 1
  • 14
  • 28
  • +1 for picking up that I missed @ PrepareForTest(Nucleus.class). I'm using TestNG as my testing framework so @ RunWith doesn't help me at the moment. I suspect I need to use @ ObjectFactory to generate my test runner. Missing some configs there so will sort that out next. – radimpe Feb 21 '12 at 09:00
  • Found that I missed out the annotation: @PrepareForTest(Nucleus.class) Also needed to create the ObjectFactory: @ObjectFactory public IObjectFactory getObjectFactory() { return new PowerMockObjectFactory(); } – radimpe Feb 21 '12 at 09:52
  • What is "globalNucleusMock" here? Have you declare it somewhere? – Saurabh Jul 08 '13 at 10:05
  • @Saurabh `globalNucleusMock` was defined as a class scoped variable using the annotation and syntax: `@Mock Nucleus globalNucleusMock` – radimpe Jul 30 '13 at 18:14