Expand my Community achievements bar.

SOLVED

AEM MockitoJUnitRunner how to bind WCMUsePojo to test using AEMContext

Avatar

Level 8

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;

@Deleted Account
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

Avatar

Correct answer by
Level 10

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.

View solution in original post

3 Replies

Avatar

Level 8

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

Avatar

Correct answer by
Level 10

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.