


I tried to mock getproperties in my test classes code details below.
main class:
public class SiteXMLCreator extends WCMUse implements Controller {
private static int level;
public int getLevel() {
return level;
}
public void setLevel(int level) {
this.level = level;
}
public Page getSiteMap(Resource resource) throws Exception {
ResourceResolver resourceResolver = resource.getResourceResolver();
PageManager pm = resourceResolver.adaptTo(PageManager.class);
String path_to_page =getProperties().get("sectionparent", String.class);
Page parentProvided = pm.getPage(path_to_page);
int depth_selected = Integer.parseInt((String) getProperties().get("depth"));
.....
...//code ..
return parentProvided;
}
test class:
public class SiteXMLCreatorTest{
SiteXMLCreator underTest;
@Mock
Page page;
@Mock
PageManager pageManager;
@Mock
ValueMap properties;
@Mock
WCMUse WCMUSE_MOCK;
FileInputStream is;
@Mock
ResourceResolver resourceResolver;
@Mock
String pagepath;
@Mock
String path_to_page;
@Mock
private Resource resource;
@Before
public void setUp() throws Exception {
underTest = new SiteXMLCreator();
}
@Test
public void testGetSiteMap() throws Exception {
when(WCMUSE_MOCK.getResource()).thenReturn(resource);
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(resourceResolver.adaptTo(PageManager.class)).thenReturn(pageManager);
when(WCMUSE_MOCK.getProperties().get("sectionparent", String.class)).thenReturn(path_to_page);// here we getting null pointer exception
when(pageManager.getPage(pagepath)).thenReturn(page);
underTest.getSiteMap(resource);
}
}
WCMUse class
public abstract class WCMUse
implements Use
{
private final Logger log = LoggerFactory.getLogger(WCMUse.class);
private Bindings bindings;
public void init(Bindings scriptBindings)
{
this.bindings = scriptBindings;
try
{
activate();
}
catch (Exception e)
{
this.log.error("Failed to activate Use class", e);
}
}
public abstract void activate()
throws Exception;
public <T> T get(String name, Class<T> type)
{
Object obj = this.bindings.get(name);
try
{
return (T)type.cast(obj);
}
catch (ClassCastException e)
{
this.log.error("Failed to cast value", e);
}
return null;
}
public SightlyWCMMode getWcmMode()
{
return (SightlyWCMMode)get("wcmmode", SightlyWCMMode.class);
}
public PageManager getPageManager()
{
return (PageManager)get("pageManager", PageManager.class);
}
public Page getCurrentPage()
{
return (Page)get("currentPage", Page.class);
}
public Page getResourcePage()
{
return (Page)get("resourcePage", Page.class);
}
public ValueMap getPageProperties()
{
return (ValueMap)get("pageProperties", ValueMap.class);
}
public ValueMap getProperties()
{
return (ValueMap)get("properties", ValueMap.class);
}
public ValueMap getInheritedProperties()
{
return (ValueMap)get("inheritedPageProperties", ValueMap.class);
}
public Resource getResource()
{
return (Resource)get("resource", Resource.class);
}
public ResourceResolver getResourceResolver()
{
return getRequest().getResourceResolver();
}
}
in test class we getting null pointer exception in when(WCMUSE_MOCK.getProperties().get("sectionparent", String.class)).thenReturn(path_to_page);
pom.xml
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.resourceresolver-mock</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.jcr-mock</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.adobe.aem</groupId>
<artifactId>aem-api</artifactId>
</dependency>
Please let me know how to resolve that problem.
Views
Replies
Total Likes