Expand my Community achievements bar.

Junit 5 - Error while using AemContext

Avatar

Community Advisor

Hi All,

I've a written sample Test class to get the resource using Aemcontext. All dependencies are resolved and i'm getting below error when i execute the class.

Error:

java.lang.VerifyError: class io.wcm.testing.mock.aem.context.AemContextImpl overrides final method currentResource.(Lorg/apache/sling/api/resource/Resource;)Lorg/apache/sling/api/resource/Resource;

Below is the sample class:

import org.apache.sling.testing.mock.sling.ResourceResolverType;

import org.junit.Test;

import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.extension.ExtendWith;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import io.wcm.testing.mock.aem.junit5.AemContext;

import io.wcm.testing.mock.aem.junit5.AemContextExtension;

@ExtendWith(AemContextExtension.class)

public class CookieModelTest {

static final Logger LOG = LoggerFactory.getLogger(CookieModelTest.class);

private AemContext context = new AemContext(ResourceResolverType.JCR_OAK);

@BeforeEach

void setUp() {

context.load().json("/example-test.json", "/content");

}

@Test

void testMyCode() {

context.currentPage(context.pageManager().getPage("/content/fedex-com/products"));

context.currentResource(context.resourceResolver().getResource("/content/fedex-com/products"));

LOG.info("Path:{}", context.requestPathInfo());

}

}

FYI: I've created project using maven arctype 14 .

Please advise.

Thanks In advance

Siva

9 Replies

Avatar

Employee Advisor

That's strange. Do you have a reference to sling mocks in your POM as well? Is your AemMocks version compatible to the SlingMocks version?

Jörg

Avatar

Community Advisor

Hi Jorg,

Thanks for your response.

FYI, below are dependencies added in project root and core level pom.

------------------------------------------- Root/pom.xml ------------------------------------

<dependencies>

<dependency>

<groupId>org.junit</groupId>

<artifactId>junit-bom</artifactId>

<version>5.3.0</version>

<type>pom</type>

<scope>import</scope>

</dependency>

<!-- TEST CASES DEPENDENCIES ENDS HERE -->

<!-- OSGi Dependencies -->

<dependency>

<groupId>org.osgi</groupId>

<artifactId>osgi.core</artifactId>

<version>6.0.0</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.osgi</groupId>

<artifactId>osgi.cmpn</artifactId>

<version>6.0.0</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.osgi</groupId>

<artifactId>osgi.annotation</artifactId>

<version>6.0.1</version>

<scope>provided</scope>

</dependency>

<!-- Logging Dependencies -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.21</version>

<scope>provided</scope>

</dependency>

<!-- Adobe AEM Dependencies -->

<dependency>

<groupId>com.adobe.aem</groupId>

<artifactId>uber-jar</artifactId>

<version>6.3.0</version>

<classifier>apis</classifier>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>com.adobe.cq</groupId>

<artifactId>core.wcm.components.all</artifactId>

<type>zip</type>

<version>2.0.4</version>

</dependency>

<!-- Apache Sling Dependencies -->

<dependency>

<groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.models.api</artifactId>

<version>1.0.0</version>

<scope>provided</scope>

</dependency>

<!-- Servlet API -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

<version>2.5</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

<version>2.1</version>

<scope>provided</scope>

</dependency>

<!-- JCR -->

<dependency>

<groupId>javax.jcr</groupId>

<artifactId>jcr</artifactId>

<version>2.0</version>

<scope>provided</scope>

</dependency>

<!-- Taglibs -->

<dependency>

<groupId>com.day.cq.wcm</groupId>

<artifactId>cq-wcm-taglib</artifactId>

<version>5.7.4</version>

<scope>provided</scope>

</dependency>

<!-- Testing -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-simple</artifactId>

<version>1.7.21</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-core</artifactId>

<version>2.7.22</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>junit-addons</groupId>

<artifactId>junit-addons</artifactId>

<version>1.4</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.testing.sling-mock</artifactId>

<version>2.2.18</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>uk.org.lidalia</groupId>

<artifactId>slf4j-test</artifactId>

<version>1.0.1</version>

<scope>test</scope>

</dependency>

</dependencies>

----------------------------------------------- CORE/pom.xml ---------------------------------------------------

<dependencies>

<!-- OSGi Dependencies -->

<dependency>

<groupId>org.osgi</groupId>

<artifactId>osgi.core</artifactId>

</dependency>

<dependency>

<groupId>org.osgi</groupId>

<artifactId>osgi.cmpn</artifactId>

</dependency>

<dependency>

<groupId>org.osgi</groupId>

<artifactId>osgi.annotation</artifactId>

</dependency>

<!-- Other Dependencies -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

</dependency>

<dependency>

<groupId>javax.jcr</groupId>

<artifactId>jcr</artifactId>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>servlet-api</artifactId>

</dependency>

<dependency>

<groupId>com.adobe.aem</groupId>

<artifactId>uber-jar</artifactId>

<classifier>apis</classifier>

</dependency>

<dependency>

<groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.models.api</artifactId>

</dependency>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

</dependency>

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-core</artifactId>

</dependency>

<dependency>

<groupId>junit-addons</groupId>

<artifactId>junit-addons</artifactId>

</dependency>

<dependency>

<groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.testing.sling-mock</artifactId>

</dependency>

<dependency>

<groupId>uk.org.lidalia</groupId>

<artifactId>slf4j-test</artifactId>

</dependency>

<!-- Junit5 Test cases Dependencies -->

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter-api</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter-params</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.junit.jupiter</groupId>

<artifactId>junit-jupiter-engine</artifactId>

<scope>test</scope>

</dependency>

<!-- Only required for JUnit 4 compatibility -->

<dependency>

<groupId>org.junit.vintage</groupId>

<artifactId>junit-vintage-engine</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>io.wcm</groupId>

<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>

<version>2.3.2</version>

</dependency>

<dependency>

<groupId>io.wcm</groupId>

<artifactId>io.wcm.testing.aem-mock.junit4</artifactId>

<version>2.3.2</version>

</dependency>

</dependencies>

Could you please advise if i'm missing any dependency here.

Thanks In advance

Siva

Avatar

Employee Advisor

You have the aemMocks for both junit4 and junit5 in your class path. You should remove this dependency:

<dependency>

<groupId>io.wcm</groupId>

<artifactId>io.wcm.testing.aem-mock.junit4</artifactId>

<version>2.3.2</version>

</dependency>

then you should also update the sling-mock dependency to a junit5 version:

old:

<dependency>

<groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.testing.sling-mock</artifactId>

<version>2.2.18</version>

<scope>test</scope>

</dependency>

new:

<dependency>

<groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>

<version>2.2.18</version>

<scope>test</scope>

</dependency>

Avatar

Community Advisor

Hi Jorg,

Thank you very much for your support.

I've updated dependency as suggested and getting below error;

java.lang.RuntimeException: Unable to initialize JCR_OAK resource resolver factory: Unable to instantiate resourcer resolver: org.apache.sling.testing.mock.sling.oak.OakMockResourceResolverAdapter. Make sure this maven dependency is included: org.apache.sling:org.apache.sling.testing.sling-mock-oak

And Tried to add below sling oak dependency but didn't work.

<dependency>

<groupId>org.apache.sling</groupId>

<artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>

<version>2.0.2</version>

<scope>test</scope>

</dependency>

Please advise, if i'm missing anything.

Thanks In advance

Siva

Avatar

Employee Advisor

Do you have any exception which indicates what is not working?

Can't you use the the ResourceResolverType JCR_MOCK? JCR_OAK is typically not required unless you really need all the features of a JCR for the tests.

Avatar

Level 1

Did you find any solution i am also having the same error when i added aemmock for sling model tests

Avatar

Community Advisor

Thanks Jorg for the response, tried with JCR_OAK as well, didn't work.

But for now, I could able to write test case using Junit 4.

Avatar

Level 9

We have the same issue with junit5.  Did you find a solution?

Avatar

Level 2

Hello, Any solution was found for above issue? I am facing the same issue. I only have junit5 dependencies in my pom but same issue.