AEM MockitoJUnitRunner how to bind WCMUsePojo to test using AEMContext

Avatar

Avatar
Validate 25
Level 4
srinivas_chann1
Level 4

Likes

21 likes

Total Posts

222 posts

Correct reply

6 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile

Avatar
Validate 25
Level 4
srinivas_chann1
Level 4

Likes

21 likes

Total Posts

222 posts

Correct reply

6 solutions
Top badges earned
Validate 25
Validate 10
Validate 1
Ignite 5
Ignite 3
View profile
srinivas_chann1
Level 4

13-06-2020

Hi,

Could some one provide inputs how could i bind my component to WCMUsePojo using AEMContext.
I am getting below error based on my component and the test case

java.lang.NullPointerException
at com.adobe.cq.sightly.WCMUsePojo.get(WCMUsePojo.java:108)
at com.adobe.cq.sightly.WCMUsePojo.getCurrentPage(WCMUsePojo.java:142)

 

public class Myclass extends WCMUsePojo {

 

@Override
public void activate() throws Exception {
....
getCurrentPage()
getRequest()
getResponse()
getResource()
private ValueMap valmAP=getProperties();
.....
}



In test class:-

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {

@InjectMocks
private Myclass myclass;

@rule
public final AemContext context = new AemContext(ResourceResolverType.JCR_MOCK);

 

 

@Before
public void init() throws Exception {
myclass = new Myclass();
}

@test
public void testActiavate(){

myclass.activate();

assertEquals(myclass .getPath(),"/content/testing");


}

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Validate 1
MVP
Theo_Pendle
MVP

Likes

238 likes

Total Posts

251 posts

Correct reply

105 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 10
Ignite 1
View profile

Avatar
Validate 1
MVP
Theo_Pendle
MVP

Likes

238 likes

Total Posts

251 posts

Correct reply

105 solutions
Top badges earned
Validate 1
Ignite 5
Ignite 3
Ignite 10
Ignite 1
View profile
Theo_Pendle
MVP

15-06-2020

Hi @srinivas_chann1,

Although @Arun_Patidar's answer is correct for what you want to do, I can't help but feel that by the time you've added the dependencies, familiarized yourself with PowerMockito and written all the tests as per the tutorial he gave you (which are is quite a lot of code), it would have been faster to simply migrate from WCMUsePojo to a Sling Model... and at least then you'd be future-proofing your code 🙂   

Also, if you're only just writing the unit tests now, that makes me feel like you probably also just created the component? If so, then there's the perfect opportunity to move towards Sling Models without having to deal with legacy code.

Answers (1)

Answers (1)

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,462 likes

Total Posts

3,329 posts

Correct reply

949 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,462 likes

Total Posts

3,329 posts

Correct reply

949 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

14-06-2020