How to write J unit test case for AEM Component | Community
Skip to main content
April 27, 2022
Solved

How to write J unit test case for AEM Component

  • April 27, 2022
  • 3 replies
  • 3638 views

Code For Reference - 

public int[] getMomentYearsArr() {
int[] years = new int[this.moments.size()];
for (int i = 0; i < this.moments.size(); i++) {
OurHistoryMoments moment = this.moments.get(i);
years[i] = moment.getMomentYear();
}
return years;
}
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by ArpitVarshney

Hi @nitish_mishra 
Don't get confused with unit testing for the AEM component. The code snippet you shared seems to be normal java code and you can easily write the Junits for the same with the help of Junits with the mockito framework. These two are very popular dependencies and ideally, your project should have already used this dependency.

Based on the JUnit version used in the project, you can understand what features you can use in test cases. There are plenty of blogs available, you can refer to this https://www.vogella.com/tutorials/Mockito/article.html 

And Just for your information, There is an Open Source project "wcm.io" which provides libraries and extensions for AEM-based applications which give us an easy way to mock things such as resources, AEM pages, OSGi services, and many other things but that can be used when it's required. You can find in-depth detail here https://wcm.io/testing/aem-mock/


Regards,

Arpit Varshney

3 replies

MayurSatav
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 27, 2022

Hi @nitish_mishra ,

 

You can write a Junit test case for the above method by doing strict stubbing using Mockito.lenient().

Refer to below blog for a clear understanding

https://aemsimplifiedbynikhil.wordpress.com/tag/mockito/

 

 

 

Kiran_Vedantam
Community Advisor
Community Advisor
April 27, 2022
ArpitVarshney
Community Advisor
ArpitVarshneyCommunity AdvisorAccepted solution
Community Advisor
April 27, 2022

Hi @nitish_mishra 
Don't get confused with unit testing for the AEM component. The code snippet you shared seems to be normal java code and you can easily write the Junits for the same with the help of Junits with the mockito framework. These two are very popular dependencies and ideally, your project should have already used this dependency.

Based on the JUnit version used in the project, you can understand what features you can use in test cases. There are plenty of blogs available, you can refer to this https://www.vogella.com/tutorials/Mockito/article.html 

And Just for your information, There is an Open Source project "wcm.io" which provides libraries and extensions for AEM-based applications which give us an easy way to mock things such as resources, AEM pages, OSGi services, and many other things but that can be used when it's required. You can find in-depth detail here https://wcm.io/testing/aem-mock/


Regards,

Arpit Varshney