Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Test Cases for sling model class

sharsha5545
Level 2
Level 2

Hi team,

I have requirement to write test cases for sling model class. I have never written any test cases. I have gone through online but there is no enough information. I am posting my sample sling model class. Can anyone help me know the procedure to write test cases.

FYI those are Page properties

public class AMPSyndicationValues {

  private Logger LOG = LoggerFactory.getLogger(AMPSyndicationValues.class);

  @Inject

  @Optional

  @Source("sling-object")

  SlingHttpServletRequest request;

  @Inject

  @Optional

  @Via("resource")

  String appleNews;

  @Inject

  @Optional

  @Via("resource")

  String googleAmp;

  @Inject

  @Optional

  @Via("resource")

  String googleAmpUrl;

  private String reqPath;

  private String ampUrl;

  @PostConstruct

  public void init() {

  Resource resource = request.getResource();

  if (null != resource) {

  reqPath = resource.getPath();

  reqPath = reqPath.replaceAll("/content/....../e[n|s]/home/", "");

  reqPath = reqPath.replaceAll("/content/..../e[n|s]/home/", "");

  reqPath = reqPath.replaceAll("/jcr:content", ".html");

  ampUrl = (googleAmpUrl == null) ? "https://feeds.test.org/" + reqPath + "?_amp=true" : googleAmpUrl;

  }

  }

  public String getAmpUrl() {

  return ampUrl;

  }

  public String getGoogleAmp() {

  return googleAmp;

  }

}

2 Replies
vaibhavs7024655
Level 3
Level 3

sharsha5545​ I would suggest you to use JUNIT for writing the test cases. Please refer the Document https://wcm.io/testing/aem-mock/usage.html  for getting started with it. Please note that this doc also has an example for writing test case in Sling Models.