Expand my Community achievements bar.

SOLVED

What are the best ways to unit test java classes?

Avatar

Level 4

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!

1 Accepted Solution

Avatar

Correct answer by
Community Advisor
7 Replies

Avatar

Level 7

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

Spock

Avatar

Level 4

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

Avatar

Level 10

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

Avatar

Level 4

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-Applicati...

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.

Avatar

Correct answer by
Community Advisor

Avatar

Level 10

Great community content that you referenced here.