Expand my Community achievements bar.

SOLVED

exception on creating test case for CQ5 component-raising from getResource() method

Avatar

Level 1

Hi,

while i am trying to get current node value by mocking node class and ResourceResolver class I am getting nullPointerException over there.Exception is raising from getResource() method.

 

Here is my actual class code

public class Myclass extends WCMUse {

public void activate() throws Exception {

Node currentNode = getResource().adaptTo(Node.class);

.

.

..........bla bla

}

}

 

Here is my Test class

    
    @Mock
    Node node;
    @Mock 
    ResourceResolver resourceResolver;
    

    @Test
        
        Resource res=resourceResolver.getResource("/");
        EasyMock.expect(res.adaptTo((Class<Node>) EasyMock.anyObject())).andReturn(node);

 

In that underlined part I am getting null pointer Exception.

Please suggest me the exact approach.
        
       

1 Accepted Solution

Avatar

Correct answer by
Level 10

See this community article that talks about using Sling API to get resources:

https://helpx.adobe.com/experience-manager/using/using-sling-apis.html

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

See this community article that talks about using Sling API to get resources:

https://helpx.adobe.com/experience-manager/using/using-sling-apis.html

Avatar

Level 1

Hi Thanks for your reply 

But I am facing that nullPointerException issue while creating Unit testcases for the component plz help me on that