AEM MockitoJUnitRunner how to bind WCMUsePojo to test using AEMContext | Community
Skip to main content
srinivas_chann1
Level 7
June 13, 2020
Solved

AEM MockitoJUnitRunner how to bind WCMUsePojo to test using AEMContext

  • June 13, 2020
  • 2 replies
  • 2431 views

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 {

 

@9944223
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;

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

 

 

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

@2785667
public void testActiavate(){

myclass.activate();

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


}

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Theo_Pendle

Hi @srinivas_chann1,

Although @arunpatidar'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.

2 replies

arunpatidar
Community Advisor
Community Advisor
June 14, 2020
srinivas_chann1
Level 7
June 15, 2020

Thanks for the inputs . Based on the link it uses PowerMockRunner.class . 

 

Below the component = PowerMockito.mock(componentClass);  is not working any inputs as what could be the reason

 

import org.apache.poi.ss.formula.functions.T;
import org.powermock.api.mockito.PowerMockito;

import java.lang.reflect.ParameterizedType;
import java.util.HashMap;

protected T component;
protected final Class componentClass = (Class) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
component = PowerMockito.mock(componentClass);

Theo_Pendle
Theo_PendleAccepted solution
Level 8
June 15, 2020

Hi @srinivas_chann1,

Although @arunpatidar'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.