AEM MockitoJUnitRunner using powermockito for testing components using WCMUsePojo for aemcontext | Community
Skip to main content
srinivas_chann1
Level 7
June 19, 2020
Solved

AEM MockitoJUnitRunner using powermockito for testing components using WCMUsePojo for aemcontext

  • June 19, 2020
  • 2 replies
  • 3383 views

Hi,

i am referring the link https://www.exadel.com/news/aem-tip-junit-tests-for-wcmusepojo-objects/

 

I am working on adding the abstract 

 

Below the code is not resolving in IDE

component = PowerMockito.mock(componentClass); 

 

any inputs as what could be the reason and how to resolve it

 

 

 

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

 

========================================================

Below is the actual class

 

 

import com.adobe.cq.sightly.SightlyWCMMode;
import io.wcm.testing.mock.aem.junit.AemContext;
import org.apache.poi.ss.formula.functions.T;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.servlet.MockRequestPathInfo;
import org.junit.Before;
import org.powermock.api.mockito.PowerMockito;

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

import static org.powermock.api.mockito.PowerMockito.when;

 

 

public abstract class AbstractComponenPowerJuTest {

protected T component;
protected ValueMap valueMap;
protected SlingScriptHelper slingScriptHelper;
protected SlingHttpServletRequest slingHttpServletRequest;
protected ResourceResolver resourceResolver;
protected SightlyWCMMode sightlyWCMMode;
protected Resource resource;
protected MockRequestPathInfo requestPathInfo;
private boolean firstRun = true;

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

protected final AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

protected void runOnceBeforeTests() {}

@Before
public void setUp() throws Exception {
runOnce();
component = PowerMockito.mock(componentClass);
resource = PowerMockito.mock(Resource.class);

resourceResolver = context.resourceResolver();
valueMap = new ValueMapDecorator(new HashMap<>());

slingScriptHelper = PowerMockito.mock(SlingScriptHelper.class);
slingHttpServletRequest = PowerMockito.mock(SlingHttpServletRequest.class);
sightlyWCMMode = PowerMockito.mock(SightlyWCMMode.class);

when(component.getResource()).thenReturn(resource);
when(resource.getResourceResolver()).thenReturn(resourceResolver);
when(component.getResourceResolver()).thenReturn(resourceResolver);

when(component.getProperties()).thenReturn(valueMap);
when(component.getSlingScriptHelper()).thenReturn(slingScriptHelper);
when(component.getRequest()).thenReturn(slingHttpServletRequest);
when(component.getWcmMode()).thenReturn(sightlyWCMMode);

requestPathInfo = new MockRequestPathInfo();
when(slingHttpServletRequest.getRequestPathInfo()).thenReturn(requestPathInfo);
when(slingHttpServletRequest.getResourceResolver()).thenReturn(resourceResolver);
}

private void runOnce() {
if (firstRun) {
firstRun = false;
runOnceBeforeTests();
}
}
}
}

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 joerghoh

Unfortunately the link does not mention if you have to make adjustments to your pom.xml to include powermock.

 

You should augment your dependencies with a block like this:

 

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.7</version>

<scope>test</scope>
</dependency>

2 replies

srinivas_chann1
Level 7
June 20, 2020

Any inputs on the issue

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
June 22, 2020

Unfortunately the link does not mention if you have to make adjustments to your pom.xml to include powermock.

 

You should augment your dependencies with a block like this:

 

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.7</version>

<scope>test</scope>
</dependency>

srinivas_chann1
Level 7
June 27, 2020

The issue is still not solved .I had also add

 

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.4</version>
<scope>test</scope>
</dependency>

 

But the actual issue is  

 

Below the code is not resolving in IDE

component = PowerMockito.mock(componentClass); 

 

Here component is not resolving 

 

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

component = PowerMockito.mock(componentClass);