AEM MockitoJUnitRunner using powermockito for testing components using WCMUsePojo for aemcontext

Avatar

Avatar

srinivas_chann1

Avatar

srinivas_chann1

srinivas_chann1

19-06-2020

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

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
Jörg_Hoh
Employee

21-06-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>

Answers (1)

Answers (1)

Avatar

Avatar

srinivas_chann1

Avatar

srinivas_chann1

srinivas_chann1

19-06-2020

Any inputs on the issue