What are the best ways to unit test java classes? | Community
Skip to main content
Level 3
August 2, 2017
Solved

What are the best ways to unit test java classes?

  • August 2, 2017
  • 7 replies
  • 3979 views

Hi,

I've gone through Sling and AEM Mocks but none of those seem helpful to test our custom java classes which has services defined by us along with resource, jcr nodes, session etc. Sling and AEM mock only test Sling and JCR apis. Is there any way to test normal java classes as well along with it? Normal Junit with mockito and powermockito requires a lot more effort and we don't want to do that.

Any good examples would be helpful.

Thanks!

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 Singaiah_Chintalapudi

Yes. It should be good enough. We've used Mockito for our code coverage.

Test an AEM sling servlet using aem-mocks – AEM Quick Snippets

https://www.practicalaem.com/2016/01/19/testing-using-mockito/

Usage | AEM Mocks

7 replies

viveksachdeva
Community Advisor
Community Advisor
August 2, 2017

Try Spock.. It is really expressive and eases writing Test cases..

Spock

vitis90Author
Level 3
August 2, 2017

do you have any examples for it? Ever tried AEMMock?

smacdonald2008
Level 10
August 2, 2017

See this GEMS webinar - it shows best practice with respect to JUNIT testing with AEM -- From Unit Testing to Integration Test of an AEM Application

kautuk_sahni
Community Manager
Community Manager
August 3, 2017
vitis90Author
Level 3
August 3, 2017

Thank you for replying.

I’ve already gone through this document

https://docs.adobe.com/content/ddc/en/gems/From-Unit-Testing-to-Integration-Test-of-an-AEM-Application/_jcr_content/par/download/file.res/AEM%20GEMS%20-%20Testing%2012-14-2016.pdf

But AEM Mocks and AEM Context seem more likely to test content, register OSGI services and AEM components only.

My question was - if we have normal java objects (which belong to different APIs) as well along with Sling and JCR APIs in a Java class do we have to use Mockito along with it, like we do it for normal junits or AEM Mocks would be sufficient?

If we can get some examples it’d be great.

Singaiah_Chintalapudi
Singaiah_ChintalapudiAccepted solution
Level 7
August 3, 2017
smacdonald2008
Level 10
August 3, 2017

Great community content that you referenced here.