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
Views
Replies
Total Likes
Can you please share with us complete error message/log?
~kautuk
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
did you get Node object as Null or something else?
Views
Replies
Total Likes
Yes it was null.
Views
Replies
Total Likes
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?
Views
Likes
Replies