AEM MockitoJUnitRunner how to bind WCMUsePojo to test using AEMContext

srinivas_chann1

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)

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)

Arun_Patidar

MVP

14-06-2020