Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM MockitoJUnitRunner how to bind WCMUsePojo to test using AEMContext

srinivas_chann1
Level 6
Level 6

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");


}

1 Accepted Solution
Theo_Pendle
Correct answer by
Community Advisor
Community Advisor

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.

View solution in original post

3 Replies
srinivas_chann1
Level 6
Level 6

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
Correct answer by
Community Advisor
Community Advisor

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.

View solution in original post