Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.
SOLVED

Unable to mock Node on Java 17 AEM 6.5 LTS

Avatar

Level 2

Hello AEM Community,

We've recently migrated our environment from AEM 6.5 to AEM 6.5 LTS. Post-migration, we've observed that several of our previously passing test cases are now failing. These failures are impacting our deployment pipeline, and we're seeking insights to resolve them.

Details:

  • Migration Overview: Transitioned from AEM 6.5 to AEM 6.5 LTS following Adobe's recommended procedures.

  • Test Framework: Utilizing JUnit 5 for unit testing.

  • Issue Observed: Test cases that involve component initialization and resource adaptation are failing. Specifically, tests that previously passed are now encountering NullPointerException during the execution of the init() method in our models.

public final AemContext context = new AemContext(JCR_MOCK);

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


<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.6.10</version>
<exclusions>
<exclusion>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.scripting.api</artifactId>
</exclusion>
</exclusions>
</dependency>
Topics

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

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @PrakashBa2 ,

 

Try to update dependency to latest one: 

<!-- https://mvnrepository.com/artifact/org.apache.sling/org.apache.sling.testing.sling-mock.junit5 -->
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
    <version>3.5.4</version>
    <scope>test</scope>
</dependency>

Try to add dependency to org.osgi artifact. The class where compiler can't find constructor is in that library.

<!-- https://mvnrepository.com/artifact/org.osgi/osgi.core -->
<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>osgi.core</artifactId>
    <version>8.0.0</version>
    <scope>provided</scope>
</dependency>

 

Best regards,

Kostiantyn Diachenko.

View solution in original post

9 Replies

Avatar

Community Advisor

Hi @PrakashBa2 ,

 

Could you please provide next details about your failing tests?

- Example of mock initialization

- You init method where you are facing exception

- full stack trace of exception

 

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

 

Best regards,

Kostiantyn Diachenko.

Avatar

Level 2
@BeforeEach
public void init() throws Exception {
context.registerService(Replicator.class, replicator);
context.registerService(XXXService.class, xxxService);
lenient().when(resolverFactory.getServiceResourceResolver(anyMap())).thenReturn(resourceResolver);
lenient().when(resourceResolver.adaptTo(PageManager.class)).thenReturn(pageManager);
lenient().when(resourceResolver.adaptTo(Session.class)).thenReturn(session);
lenient().when(res.adaptTo(Node.class)).thenReturn(node);
lenient().when(resourceResolver.adaptTo(TagManager.class)).thenReturn(tagManager);
}
java.lang.RuntimeException: Unable to initialize JCR_MOCK resource resolver factory: Unable to invoke method 'activate' for class org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator
 
at io.wcm.testing.mock.aem.context.ContextResourceResolverFactory.get(ContextResourceResolverFactory.java:84)
at io.wcm.testing.mock.aem.context.AemContextImpl.newResourceResolverFactory(AemContextImpl.java:104)
at org.apache.sling.testing.mock.sling.context.SlingContextImpl.resourceResolverFactory(SlingContextImpl.java:208)
at org.apache.sling.testing.mock.sling.context.SlingContextImpl.setUp(SlingContextImpl.java:192)
at io.wcm.testing.mock.aem.context.AemContextImpl.setUp(AemContextImpl.java:109)
at io.wcm.testing.mock.aem.junit5.AemContext.setUpContext(AemContext.java:100)
at io.wcm.testing.mock.aem.junit5.AemContextExtension.setAemContextInStore(AemContextExtension.java:73)
at io.wcm.testing.mock.aem.junit5.AemContextExtension.postProcessTestInstance(AemContextExtension.java:63)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:183)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
at java.base/java.util.Optional.orElseGet(Optional.java:364)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: java.lang.RuntimeException: Unable to invoke method 'activate' for class org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator
at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.invokeMethod(OsgiServiceUtil.java:379)
at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.lambda$invokeLifecycleMethod$0(OsgiServiceUtil.java:262)
at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.findAndInvokeNearestMethod(OsgiServiceUtil.java:141)
at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.invokeLifecycleMethod(OsgiServiceUtil.java:177)
at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.activateDeactivate(OsgiServiceUtil.java:88)
at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.activateInjectServices(OsgiServiceUtil.java:503)
at org.apache.sling.testing.mock.osgi.MockOsgi.registerInjectActivateService(MockOsgi.java:299)
at org.apache.sling.testing.mock.sling.ResourceResolverFactoryInitializer.initializeResourceResolverFactoryActivator(ResourceResolverFactoryInitializer.java:174)
at org.apache.sling.testing.mock.sling.ResourceResolverFactoryInitializer.setUp(ResourceResolverFactoryInitializer.java:93)
at org.apache.sling.testing.mock.sling.MockSling.buildFactoryFromRepository(MockSling.java:115)
at org.apache.sling.testing.mock.sling.MockSling.newResourceResolverFactory(MockSling.java:97)
at io.wcm.testing.mock.aem.context.ContextResourceResolverFactory.get(ContextResourceResolverFactory.java:56)
... 21 more
Caused by: java.lang.NoSuchMethodError: 'void org.osgi.util.tracker.ServiceTracker.<init>(org.osgi.framework.BundleContext, java.lang.Class, org.osgi.util.tracker.ServiceTrackerCustomizer)'
at org.apache.sling.resourceresolver.impl.observation.ResourceChangeListenerWhiteboard.activate(ResourceChangeListenerWhiteboard.java:62)
at org.apache.sling.resourceresolver.impl.ResourceResolverFactoryActivator.activate(ResourceResolverFactoryActivator.java:407)
at java.base/java.lang.reflect.Method.invoke(Method.java:569)
at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.invokeMethod(OsgiServiceUtil.java:364)
... 32 more

Avatar

Correct answer by
Community Advisor

Hi @PrakashBa2 ,

 

Try to update dependency to latest one: 

<!-- https://mvnrepository.com/artifact/org.apache.sling/org.apache.sling.testing.sling-mock.junit5 -->
<dependency>
    <groupId>org.apache.sling</groupId>
    <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
    <version>3.5.4</version>
    <scope>test</scope>
</dependency>

Try to add dependency to org.osgi artifact. The class where compiler can't find constructor is in that library.

<!-- https://mvnrepository.com/artifact/org.osgi/osgi.core -->
<dependency>
    <groupId>org.osgi</groupId>
    <artifactId>osgi.core</artifactId>
    <version>8.0.0</version>
    <scope>provided</scope>
</dependency>

 

Best regards,

Kostiantyn Diachenko.

incresing version osgi.core resolves the issues Thank you

Avatar

Community Advisor

Hi @PrakashBa2,

Your test cases are failing due to incompatible or outdated test dependencies after moving to AEM 6.5 LTS.

Try below steps steps to fix it

  1. Update test dependencies in your pom.xml:

<dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.sling-mock.junit5</artifactId>
  <version>3.6.4</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>io.wcm</groupId>
  <artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
  <version>5.6.14</version>
  <scope>test</scope>
</dependency>
  1. Explicitly register your models in the test:

context.addModelsForClasses(MyModel.class);
  1. Avoid NPEs in init():
    Wrap logic defensively:

@PostConstruct
protected void init() {
  if (someValue != null) {
    // safe usage
  }
}
  1. Ensure test resource includes required properties:

context.create().resource("/content/test", "title", "Hello");

I hope that should resolve most post-migration test failures.


Santosh Sai

AEM BlogsLinkedIn


Avatar

Level 2
 <version>3.6.4</version>
 <version>5.6.14</version>

are not exists

Avatar

Community Advisor

@PrakashBa2 Could you check the latest version? Posted above code for your understanding.

 


Santosh Sai

AEM BlogsLinkedIn


Avatar

Community Advisor

Hi @PrakashBa2 ,

The root cause of your issue:

Caused by: java.lang.NoSuchMethodError: 
  'void org.osgi.util.tracker.ServiceTracker.<init>(org.osgi.framework.BundleContext, java.lang.Class, org.osgi.util.tracker.ServiceTrackerCustomizer)'

means you're facing binary incompatibility between the org.osgi library and Apache Sling testing libraries when using Java 17 with JCR_MOCK in AEM 6.5 LTS.

This is a known problem due to outdated OSGi versions bundled in older Sling/AEM Mock libraries not working with Java 17’s stricter module access and updated APIs.

1. STOP Using JCR_MOCK

This mode is not Java 17 compatible anymore due to deep reflection and OSGi tracking issues.

Instead, use:

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

This works with Java 17 and is safe unless you truly need a deep JCR node tree simulation (e.g., for custom node types or replication logic).

2. Upgrade Dependencies

Use the latest versions fully compatible with Java 17:

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

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

You can verify these exist in Maven Central — both versions are valid and public.

3. Use Defensive Null Checks in @PostConstruct

@PostConstruct
protected void init() {
    if (resource != null && resource.adaptTo(Node.class) != null) {
        Node node = resource.adaptTo(Node.class);
        // do your logic here safely
    }
}

4. Define Java Version Explicitly in pom.xml

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

 

Regards,
Amit

Avatar

Level 2

Hey Amit thank you for reply, we are using JCR API in code base ResourceResolverType.RESOURCERESOLVER_MOCK will return Null to JCR API