Friday, December 9, 2016

How to Write Tests in AEM

Use Case: Writing tests for AEM application.

Current Issue: As your project and code base grows, it is really important to make sure that test coverage for code is there to maintain consistency and sanity of your code. Writing test cases for AEM is little bit different than writing conventional Java test cases, This makes it difficult for beginner to write test cases for AEM application.

Idea of this post to give different options available for writing unit test for AEM services.

Good to know:
I would explain how you can have better test coverage for your application by giving different use cases,

Dependencies: It is recommended to have following dependencies in to your pom before start writing for tests for your application
Case 1: Writing test cases for Generic Helper class.

This is simplest use case where your generic helper class (For example StringUtils, DateUtils) is not using any AEM libraries. For this you can simply use Junit to write your unit test.

Here is very simple example:

Case 2: Writing test cases for AEM Helper class

This is second use case where you want to test AEM helper methods. For this you can use combination of Junit and Mockito. Use Mockito to Mock AEM services and methods and Junit for assertion.

Here is simple example

Case 3: Writing test cases for AEM services

Now it gets little bit tricky where you need to mock certain behavior of bundle and implicit object. That's why Sling has created Mock version of sling objects and has created mock version of AEM objects. You can just use aem mock to achieve most of your use cases. (AEM mock extend Sling mock).

here are some of the common use cases you will come across while testing your service.

1) How can I mock content my service is running against ?

For this it is recommended to use contentLoader API to either load existing json based resource (You can simply get it by creating resource in CRXDE and then using something like RESOURCEPATH.infinity.json to get json for that resource) or just create mock resource using ContentBuilder context.create().resource() or ResourceBuilder

Note that if you are mocking Page object then you have to use aem mock using aemcontext.pageManager().create()

2) How Can I initialize properties in the bundle ?

You can use register and activate OSGI service with properties for that. Here is some example

3) How Can I inject other service for my service ?

You can either Mock service or use register service API for that

Note that when you inject a service to your service using Reference then you have to register this your injected service, otherwise your test will fail.

4) How Can I test sling model ?

You can use aemContext for that.

Case 4: Writing test cases for AEM servlets and filters

This is very similar to how you would do test cases for Service. For request and response you either have to mock request / response object using Mockito or Use Spy  or use sling request and response mock. Since a lot of methods in filter and servlet do not return any result, Make Mockito verify your friend.  Here is one example using simple mockito to test servlet

 How can I measure my test coverage ?

 You can use jococo test coverage plugin along with your build system to measure this coverage. You can have following plugin in to your parent pom

How Can I write Integration test in AEM ?

Very good example here

It is based of sling test base

I know this information is not enough to have you set up for writing tests in AEM. Feel free to let me know if you have more question and I will add more stuff here.


  1. Thank you for sharing your thoughts i realy like it the blog is very good thanks.
    we are best web development company
    if you want any kind of service please contact thanks.

  2. I am feeling excited after reading your post, to sell pets online time your time to visit buy pet online in india. thank you

  3. I just want say, you explained this topic very well. Website is must now for every business and writer to express the information so we are Web Development Company Chicago here to help you Digital Marketing Agency Chicago. thank for the post and all the best

  4. thank you for helping us by sharing the these important tricks. we offer the packers and movers in Ernakulam and Packers and Movers in Kochi thanks again

  5. It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command.

    software testing course in chennai

  6. Jio-fi is a wifi enabling device which is actually a hotspot router in which a sim can be inserted. It is sleekly designed such that you can carry it wherever you want to favour its portability.

    How to Change Jiofi Password


  7. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    Software testing course in chennai

  8. JCPenney is a chain of departmental stores at over 900 locations in the USA. JCPenney Company was founded in 1902 by James Cash Penney and William Henry

    JCPenney Associate Kiosk


  9. This post is on the LiteBlue USPS Gov Human Resources benefits that are for every one of the representatives of the biggest mailing administration. You are certain to have caught wind of the United States Postal Services and their accomplishments. It is an all around incorporated postal administration over the entire country. The Human Resource Division enables the workers to get to their advantages and other essential administrations from the USPS organization.
    Lite Blue