Junit 5 - Error while using AemContext | Community
Skip to main content
Siva_Sogalapalli
Community Advisor
Community Advisor
October 30, 2018

Junit 5 - Error while using AemContext

  • October 30, 2018
  • 2 replies
  • 11365 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

joerghoh
Adobe Employee
Adobe Employee
October 31, 2018

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

Siva_Sogalapalli
Community Advisor
Community Advisor
October 31, 2018

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

joerghoh
Adobe Employee
Adobe Employee
November 1, 2018

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>

New Member
November 20, 2018

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

Siva_Sogalapalli
Community Advisor
Community Advisor
November 27, 2018

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.

Level 8
May 28, 2023

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