Expand my Community achievements bar.

SOLVED

JCR MOCK Fails on Java 17 while Migrating to AEM 6.5 LTS

Avatar

Level 2

Error java.lang.RuntimeException: Unable to initialize JCR_MOCK resource resolver factory: Unable to invoke method 'activate' for class org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Level 2
<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>osgi.core</artifactId>
    <version>8.0.0</version>
    <scope>provided</scope>
</dependency>

after updating Osgi version it resolved

View solution in original post

6 Replies

Avatar

Community Advisor

Hi @PrakashBa2 ,

 

Could you please provide next details about your failing tests?

- full stack trace of exception

- list of test dependencies

- how do you execute tests?

 

These information should help us to localize problem and advice you with a recommendation to fix.

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko



Check out AEM VLT Intellij plugin


Avatar

Level 5

HI @PrakashBa2 ,

According to my understanding, 

This error usually arises because  JCR_MOCK and related Apache Sling dependencies used in unit testing (like `ResourceResolverFactoryActivator`) are not fully compatible with Java 17, especially in older AEM setups.

 

1. You can upgrade dependencies to Java 17-Compatible Versions -  Ensure you're using the latest versions of io.wcm.testing.mock.aem, Sling libraries, and other related test dependencies that support Java 17.

2. Run Unit Tests with Java 11 (as a workaround) - 
If upgrading dependencies isn't feasible, configure your test environment to use Java 11 while keeping the main project on Java 17.

 

Let me know if it works, or please elaborate your problem that you are facing.

Thanks.

Avatar

Community Advisor

Hi @PrakashBa2,

Try below options whichever you think is best suitable according to your case  

Option 1: Switch to RESOURCE_RESOLVER_MOCK

Instead of JCR_MOCK, use:

public final AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);

This is faster, more isolated, and doesn't depend on the full JCR stack.

Use it if your test does not require JCR hierarchy simulation like page/component structures.


Option 2: Update Sling + AEM Mock Versions

Update to the latest compatible versions that better support Java 17:

<dependency>
  <groupId>io.wcm</groupId>
  <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
  <version>5.6.14</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
  <version>3.6.4</version>
  <scope>test</scope>
</dependency>

Also, ensure your project’s maven.compiler.release is set properly:

<properties>
  <maven.compiler.release>17</maven.compiler.release>
</properties>

Hope that helps!


Santosh Sai

AEM BlogsLinkedIn


Avatar

Community Advisor

Hi @PrakashBa2 ,

The issue you're facing -

java.lang.RuntimeException: Unable to initialize JCR_MOCK resource resolver factory

is a known incompatibility between JCR_MOCK and Java 17, particularly due to changes in module access restrictions and internal reflection behavior in Java 16+.

Solutions (Choose Based on Your Needs)

Option 1: Switch to RESOURCE_RESOLVER_MOCK (Recommended if possible)

@ExtendWith(AemContextExtension.class)
class MyTest {
  private final AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
}

Works well for unit tests that don’t need JCR node hierarchy simulation (e.g., when testing services, models).

Option 2: Upgrade Your Dependencies

Update these to latest Java 17-compatible versions:

<!-- AEM Mock -->
<dependency>
  <groupId>io.wcm</groupId>
  <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
  <version>5.6.14</version>
  <scope>test</scope>
</dependency>

<!-- Sling Mock -->
<dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
  <version>3.6.4</version>
  <scope>test</scope>
</dependency>

Also, make sure you're explicitly telling Maven to compile for Java 17:

<properties>
  <maven.compiler.release>17</maven.compiler.release>
</properties>

Option 3: Temporary Workaround – Run Unit Tests on Java 11

If you're stuck with older test dependencies that don’t yet fully support Java 17:

Run builds/tests using Java 11 (JAVA_HOME=...) even if the main application builds on Java 17.

Note:

Avatar

Community Advisor

@PrakashBa2 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!


Aanchal Sikka

Avatar

Correct answer by
Level 2
<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>osgi.core</artifactId>
    <version>8.0.0</version>
    <scope>provided</scope>
</dependency>

after updating Osgi version it resolved