AEM MockitoJUnitRunner с использованием powermockito для тестирования компонентов с использованием WCMUsePojo для aemcontext - PullRequest
0 голосов
/ 20 июня 2020

я имею в виду ссылку https://www.exadel.com/news/aem-tip-junit-tests-for-wcmusepojo-objects/

Я работаю над добавлением абстрактного

Ниже код не разрешается в компоненте IDE = PowerMockito .mock (componentClass); любые входные данные, в чем может быть причина и как ее решить

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



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


>>Please provide inputs

1 Ответ

0 голосов
/ 20 июня 2020

Ниже фактический класс, который я добавил, и он показывает ошибки в ide



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

...