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.
Prerequisite:
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. https://www.tutorialspoint.com/junit
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 wcm.io has created mock version of AEM objects. You can just use aem mock http://wcm.io/testing/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 http://wcm.io/testing/aem-mock/usage.html 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 context.build().resource() http://wcm.io/testing/aem-mock/apidocs/
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 http://wcm.io/testing/aem-mock/usage.html#Registering_OSGi_service 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 http://wcm.io/testing/aem-mock/usage.html#Registering_OSGi_service
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. http://wcm.io/testing/aem-mock/usage.html#Sling_Models
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 https://github.com/sneakybeaky/AEM-Integration-Test-Example
It is based of sling test base https://sling.apache.org/documentation/development/sling-testing-tools.html
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.
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.
Prerequisite:
- Familiar with Java Junit test framework http://junit.org
- Familiar with Java Mockito test framework http://site.mockito.org/
- Optional: PowerMockito test framework http://powermock.github.io/
- Sling test framework (Mock and IT) https://sling.apache.org/documentation/development.html
- AEM Mock http://wcm.io/testing/aem-mock/ and http://wcm.io/testing/aem-mock/apidocs/
- WCMUse classes Mocking http://aempodcast.com/2015/testing/unit-testing-wcmuse-classes-using-mockito/#.WEmiUMMrJKc
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. https://www.tutorialspoint.com/junit
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 wcm.io has created mock version of AEM objects. You can just use aem mock http://wcm.io/testing/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 http://wcm.io/testing/aem-mock/usage.html 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 context.build().resource() http://wcm.io/testing/aem-mock/apidocs/
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 http://wcm.io/testing/aem-mock/usage.html#Registering_OSGi_service 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 http://wcm.io/testing/aem-mock/usage.html#Registering_OSGi_service
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. http://wcm.io/testing/aem-mock/usage.html#Sling_Models
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 https://github.com/sneakybeaky/AEM-Integration-Test-Example
It is based of sling test base https://sling.apache.org/documentation/development/sling-testing-tools.html
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.
It's A Really Nice Post.Thank You For sharing.....
ReplyDeleteBest Digital Marketing Courses in Bangalore
The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Project Centers in Chennai for CSE.
DeleteSpring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai
Great Article… I love to read your articles because your writing style is too good, its is very very helpful. yoga teacher training in rishikesh
ReplyDeleteHi DEAR..Thanks for sharing this post.I think it is a transcendent post. Such a wide number of an abundance of a dedication of appreciation is all together for this data. We Are Mobile Signal Booster
ReplyDeleteget in touch with us for mobile network solution
Hi DEAR ...It was actually wonderful reading this info and I think you are absolutely right and I truly appreciate the article post thanks for sharing... WE are WYOXSPORTS
ReplyDeleteweightlifting accessories contat us for all weightlifting accessories
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end. clark county detention center 24 hour bail bonds
ReplyDeleteThis is a decent article here with some helpful hints for the individuals who are not used-to remark that much of the time. A debt of gratitude is in order for this accommodating data I concur with all focuses you have given to us. I will pursue every one of them.
ReplyDeleteAstrologer In Jaipur
Astrologer in Hyderabad
black magic specialist in bangalore
Vashikaran specialist in Mumbai
Love marriage specialist in Delhi
Vashikaran specialist in Delhi
Best Astrologer in Delhi
love marriage specialist
Astrologer in Kolkata
Love Marriage Specialist In Hyderabad
It has been essentially unfathomably liberal with you to give transparently what precisely numerous people would've promoted for an eBook to finish up making some money for their end, fundamentally given that you could have attempted it in the occasion you needed.
ReplyDeleteAdopt Pets
Adopt dogs near me
Adopt cat near me
Great Article… I love to read your articles because your writing style is too good, its is very very helpful. yoga teacher training in rishikesh
ReplyDeleteyoga ttc in rishikesh
200 hours yoga teacher training in rishikesh
Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
ReplyDeleteSEO company in coimbatore
SEO company
web design company in coimbatore
Thank you for sharing information.
ReplyDeleteSEO Company in Noida
Best SEO Company in Noida
Best SEO Services in Noida
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end. if you are looking for satyanarayan puja please visit us.
ReplyDeleteLove Problem Solution In Delhi
ReplyDeleteLove Problem Solution In Kolkata
Love Problem Solution In Punjab
Love Problem Solution In Gurgaon
Love Problem Solution In Mumbai
Love Problem Solution In Pune
Love Problem Solution In Nagpur
Love Problem Solution In Jaipur
Love Problem Solution In Udaipur
ReplyDeleteLove Problem Solution In Ajmer
Love Problem Solution In Kota
Love Problem Solution In Agra
Love Problem Solution In Ghaziabad
Love Problem Solution In Bangalore
Love Problem Solution In Rajasthan
Love Problem Solution In Chittorgarh
Love Problem Solution In Alwar
ReplyDeleteLove Problem Solution In Gandhinagar
Love Problem Solution In Rajkot
Love Problem Solution In Vadodara
Love Problem Solution In Chandigarh
Love Problem Solution In Kanpur
love problem solution guru ji
husband wife problem solution
love problem solution in california
ReplyDeletelove problem solution
love guru in delhi
husband and wife problems
Great Article… I love to read your articles because your writing style is too good, its is very very helpful. yoga teacher training in rishikesh
ReplyDeleteHello DEAR ... I really value the article post a debt of gratitude is in order for sharing... WE are Free Jobs Notifications we are here to help candidates set up automatic monitoring for Bank of Baroda Recruitment
ReplyDeleteI Really like your blog. Thanks for sharing..
ReplyDeleteWebsite Designing Company in Delhi
Zehra Global Services provides the best services Graphic Design, Software Development, Digital marketing and Mobile app development. It provides the best services to our customer.
ReplyDeleteMobile App Development
Graphic Design
Digital marketing
This comment has been removed by the author.
ReplyDeleteI found this article useful and looking forword for similar kind of blogs and if you want to know about TOP WEDDING PLANNERS CALGARY then u r at right place.
ReplyDeleteNice blogs and eagerly waiting for such blogs and if you looking forword for Affordable Web Development Services then you are right place.
ReplyDeleteNice blogs and eagerly waiting for such blogs and if you looking forword forTOP WEDDING PLANNERS CALGARY then u r at right place
ReplyDelete"Extraordinary Article… I want to peruse your articles on the grounds that your composition style is excessively great, its is extremely useful. I really value the article post a debt of gratitude is in order for sharing... WE are Ayurvedic Herbs Products get in touch with us for all Buy Wellness Products Online , Buy Ayurvedic Herbs Online
ReplyDeleteBuy Wellness Products"
Great post. I was once checking constantly this weblog and I'm impressed! Extremely useful information specially the closing part. I maintain such information much. I was once seeking this specific information for a very long time. Many thanks and best of luck you are looking for BEST NEWSMAGAZINE please visit us.
ReplyDeleteGreetings DEAR ...It was really magnificent perusing this information and I think you are totally right and I genuinely welcome the article post a debt of gratitude is in order for sharing... WE are Hazard Communication Training get in touch with us for Confined Space Entry Training , Heavy Equipment Training Program
ReplyDeleteGreat post. I was once checking constantly this weblog and I'm impressed! Extremely useful information specially the closing part. I maintain such information much. I was once seeking this specific information for a very long time. Many thanks and best of luck you are looking for BEST NEWSMAGAZINE please visit us. BEST NEWSFEED SITE
ReplyDeleteI’m really happy to say it was an interesting post to read. I learned new information from your article, you are doing a great job. Continue. FAcebook , Twitter
ReplyDeleteCANIRA Website Development in usa , We are offering corporate Website Designing Service , give secure Web Solutions, Ecommerce Design, Apps Development, Digital Marketing and More
ReplyDeleteI’m really happy to say it was an interesting post to read. I learned new information from your article, you are doing a great job. Continue. Tips to increase stamina level Free , key to weight loss
ReplyDeleteAstrology vashikaran specialist
ReplyDeleteAstrologer for love solution
Ex love back vashikaran
ReplyDeleteBreakup problem solution
Astrologer for love solution
Hi ! This is very informative & interesting article.Nice to read your blog post first time ever. I really appreciate this post. Thanks for sharing this awesome post if you are looking for Visitor Management System please visit us.
ReplyDeletethe best WordPress themes for business, blogging, and more. This accumulation of the Best Worpress Theme 2019 will enable you to build up a plan, Get your Best Matching Theme Now!
ReplyDeleteOn the off chance that u are Looking for another WordPress subject? Download the Best Wordpress Theme and layouts, Landing Page Templates , Free and Premium formats
Hariom Sharma is World Famous Astrologer. They have good knowlge about of horoscopes So If You Have Any Problem You Can Consultant Best Astrologer Hariom Sharma.
ReplyDeleteVashikaran Specialist Astrologer
Love Problem Soution
Vashikaran specialist in Bangalore
Love Marriage Specialist
Astrologer in Mumbai
Vashikaran Specialist In Australia
Vashikaran Specialist In Canada
Astrologer in New Zealand
Vashikaran Specialist in Uk
Health is Wealth”, everyone have heard about this famous quote. When you have a healthy lifestyle, Lenalidomide Cost
ReplyDeleteThis is a very elegantly composed post, my compliments. I am examining your post from the most punctual beginning stage, it was so captivating to voteize and I feel on account of you for posting such an OK blog, keep invigorates regularly. we are Designer Sarees Online
ReplyDeleteThanks a lot for sharing this blog. I was searching for this topic for a while. Glad that I came across your blog. Great effort. Do share more.we are Wallpaper importer in Delhi
ReplyDeleteWorld's greatest astrologer Karan Sharma ji available at + 91-95777-44786 ; he is also Indian's best beneficial vashikaran expert for love and marriage.
ReplyDeleteI love to visit your site repeatedly because; here I find all information is very useful and educational for me.we are Interior Designer in Delhi
ReplyDeleteThanks a lot for sharing this blog. I was searching for this topic for a while. Glad that I came across your blog. Great effort. Do share more.we are Wallpaper importer in Delhi
ReplyDeletethanks for information if u want to know more aboutCalifornia Wineskindly visit us
ReplyDeleteThis is a very elegantly composed post, my compliments. I am examining your post from the most punctual beginning stage, it was so captivating to voteize and I feel on account of you for posting such an OK blog, keep invigorates regularly. we are Beauty Parlour in Patiala Salon in Patiala
ReplyDeleteNice blog..! Really very informative and creative contents. This concept is a good way to enhance the knowledge.Car Interior Cleaning Dubai-
ReplyDeleteGreat Article.Thank you for sharing such information -best pre school in punjab
ReplyDeletethanks for information if u want to know more about kindly visit us :- Car Interior Cleaning Dubai
ReplyDeleteShopclues here came up with an Offer where you can win special Shopclues Winner List 2019 by just playing a game & win prizes Call @6299249427
ReplyDeleteShopclues Winner List 2019
Shopclues Winner List
Shopclues Prize
Prize Shopclues
Very Nice nd informative post thanku for sharing with us.
ReplyDeleteAstrologer In Toronto
Astrology In uk
Astrologer In London
Astrologer In usa
Astrologer In Canada
Astrologer In Malaysia
Black Magic Specialist
Astrologer In Hyderabad
Thanks for sharing the information.
ReplyDeleteWeb Development Company in India
Digital Marketing Company
Mobile App Development Company
Graphic Design Company
just playing a game & win prizes
ReplyDeleteShopclues Winner List 2019
Prize Shopclues
Shopclues Winner Name
Shopclues Winner Name 2019
Shopclues Winner List
Thanks for sharing .check the win prizes list .
ReplyDeleteShopclues Winner List 2020
Shopclues Winner Name 2020
Shopclues Lucky Draw 2020
Winner List Shopclues 2020
Shopclues Lucky Customer 2020
We are providing Online Architectural Services, This is the most trustworthy site for planning application and building regulation drawings.
ReplyDeleteReally this information was helpful. I Got some good idea by reading this topic. Great information thanks for sharing this post. Land registry complience Plan
ReplyDeleteAyurheals provides Best Ayurveda herbs online For Bedwetting or Diabetes with natural herbal remedies with Ayurvedic experts for diet and life style changes.Ayurvedic Herbs Online
ReplyDeleteWinner List Naaptol here came up with an Offer where you can win special Naaptol prize by just playing a game & win prizes Call @8404917617
ReplyDeletenaaptol winner list 2020
naaptol winner list
naaptol winner name 2020
naaptol winner name
naaptol prize list
naaptol lucky draw 2020
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end we are Online ayurveda
ReplyDeleteFor the best Legitimate Custom Coursework Writing Services, find the best Online Coursework Writing Service Provider for all your Custom College Coursework Services.
ReplyDeleteSnapdeal prize by just playing a game & win prizes Call @6289576795
ReplyDeletesnapdeal winner list 2020
snapdeal winner list 2020
snapdeal lucky draw
Snapdeal prize department
snapdeal winner name
snapdeal lucky customer