Expand my Community achievements bar.

SOLVED

AEM MockitoJUnitRunner using powermockito for testing components using WCMUsePojo for aemcontext

Avatar

Level 8

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

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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>

View solution in original post

4 Replies

Avatar

Correct answer by
Employee Advisor

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>

Avatar

Level 8

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

Avatar

Employee Advisor
Well, there could be many issues, why this class is not resolved properly. If the maven build works, it's a problem of your IDE. If the maven build fails with compilation issues, it would be great, if you could share the project (or a minimal sample to demonstrate the problem) on github. The Question&Answer mode which a forum imposes is not the best way to resolve this problem.