Expand my Community achievements bar.

Test Cases for sling model class

Avatar

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

Avatar

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.