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
BedrockMission!

Learn More

View all

Sign in to view all badges

Test Cases for sling model class

Avatar

Avatar
Validate 1
Level 1
sharsha5545
Level 1

Like

1 like

Total Posts

10 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile

Avatar
Validate 1
Level 1
sharsha5545
Level 1

Like

1 like

Total Posts

10 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 1
View profile
sharsha5545
Level 1

21-06-2019

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;

  }

}

Replies

Avatar

Avatar
Boost 5
Level 3
vaibhavs7024655
Level 3

Likes

18 likes

Total Posts

28 posts

Correct Reply

6 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
Affirm 5
View profile

Avatar
Boost 5
Level 3
vaibhavs7024655
Level 3

Likes

18 likes

Total Posts

28 posts

Correct Reply

6 solutions
Top badges earned
Boost 5
Boost 3
Boost 10
Boost 1
Affirm 5
View profile
vaibhavs7024655
Level 3

21-06-2019

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.

Avatar

Avatar
Give Back 5
Level 4
joeb21688728
Level 4

Likes

33 likes

Total Posts

60 posts

Correct Reply

8 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 25
Give Back 10
Give Back
View profile

Avatar
Give Back 5
Level 4
joeb21688728
Level 4

Likes

33 likes

Total Posts

60 posts

Correct Reply

8 solutions
Top badges earned
Give Back 5
Give Back 3
Give Back 25
Give Back 10
Give Back
View profile
joeb21688728
Level 4

21-06-2019