Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Unit Test for SlingQuery

Avatar

Level 3

How can I write a unit test to test below Sling Query from my code.

 

List<Resource> legalDisclaimerResourceList = $(legalDisclaimerPageResource).find(LEGAL_DISCLAIMER).asList();

 

$ refers to Sling Query from 

import static org.apache.sling.query.SlingQuery.$;

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @troubleshooter3,

Per the official Sling docs, Mock implementation for Sling Query is not available in the list of Selected Sling APIs (for which the mock is available)

https://sling.apache.org/documentation/development/sling-mock.html

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @troubleshooter3,

Per the official Sling docs, Mock implementation for Sling Query is not available in the list of Selected Sling APIs (for which the mock is available)

https://sling.apache.org/documentation/development/sling-mock.html

 

Avatar

Level 1

Hey

I am sharing new examples, just look into it , i hope you will find this helpful

For JUnit 5:

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

For JUnit 4:

<dependency>
  <groupId>org.apache.sling</groupId>
  <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
</dependency>