Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

AEM Mocks

nikunjk71293848
Level 3
Level 3

When I am using AEM Mocks for writing Junits, I am not able to perform adaptTo functionality on the resource generated from AEM context.
Its giving Invocation Target Exception.My AEM version is 6.0 and aem mock version is 1.2.4.
The things which I have tried is from the below link:
http://wcm.io/testing/aem-mock/usage.html
 

0 Replies
kautuk_sahni
Employee
Employee

Can you please share with us complete error message/log?

~kautuk

nikunjk71293848
Level 3
Level 3

kautuksahni wrote...

Can you please share with us complete error message/log?

~kautuk

 

I have used below code:

Case 1:-
Resource resource = context.create().resource("/etc/commerce/products");

 CommerceService commerceService = resource.adaptTo(CommerceService.class);

In this case we are getting commerceService as null.

Case 2:-

public final AemContext context = new AemContext();
CommerceService mockCommerce = Mockito.mock(CommerceService.class);
 context.registerService(CommerceService.class,mockCommerce);
 CommerceService service = context.getService(CommerceService.class);
  Mockito.when(resource.adaptTo(CommerceService.class)).thenReturn(service);
In this case I am getting "java.lang.reflect.InvocationTargetException"

Please help me out in both the cases.

Jitendra_S_Toma
Level 9
Level 9

Hi,

How are you creating resource object from AEM Context?. What is the AEM Context? 

Is there any specific reason not to use Sling mock features?

nikunjk71293848 wrote...

When I am using AEM Mocks for writing Junits, I am not able to perform adaptTo functionality on the resource generated from AEM context.
Its giving Invocation Target Exception.My AEM version is 6.0 and aem mock version is 1.2.4.
The things which I have tried is from the below link:
http://wcm.io/testing/aem-mock/usage.html
 

 

nikunjk71293848
Level 3
Level 3

By using AEM mocks, we get the AEM context and through that context we get resource.It is explained in below link:-

http://wcm.io/testing/aem-mock/usage.html

I tried to use Sling mocks also but it was not working.

Jitendra_S_Toma
Level 9
Level 9

I think I understand it. it seems CommerceService object does not get created for every resource. In generally,  CommerceService object gets initialized on the page who has product references.

Try to adapto Node and print Node path. Adapted Node object isn't null then need to understand how commerceService object is initiated.

Resource resource = context.create().resource("/etc/.....");

Node tempNode = resource.adapTo(Node.class);

print tempNode.getPath();

nikunjk71293848 wrote...

By using AEM mocks, we get the AEM context and through that context we get resource.It is explained in below link:-

http://wcm.io/testing/aem-mock/usage.html

I tried to use Sling mocks also but it was not working.

 

 

nikunjk71293848
Level 3
Level 3

Jitendra S.Tomar wrote...

I think I understand it. it seems CommerceService object does not get created for every resource. In generally,  CommerceService object gets initialized on the page who has product references.

Try to adapto Node and print Node path. Adapted Node object isn't null then need to understand how commerceService object is initiated.

Resource resource = context.create().resource("/etc/.....");

Node tempNode = resource.adapTo(Node.class);

print tempNode.getPath();

nikunjk71293848 wrote...

By using AEM mocks, we get the AEM context and through that context we get resource.It is explained in below link:-

http://wcm.io/testing/aem-mock/usage.html

I tried to use Sling mocks also but it was not working.

 

 

 

 

I have tried that also but it was not working.

Som_Architect
Level 1
Level 1

I see this is a relatively older post, however, I am facing same issue with AEM 6.4 and aem-mock 2.3.4.

If possible, can we have the resolution posted here for everyone's reference?