Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Junit AEM to mock a resource is returning null

Avatar

Level 2

Hi all,

 

I am trying to mock this request to return a resource, resolver.getResource("/content/project/en/home/products/p1")

Able to get the resource object inside my test class but on invoking my actual method it returns null.

Any pointers would be helpful.

 

In my Test class:

lenient().when(resourceresolver.getResource("/content/project/en/home/products/p1"))

.thenReturn(childResource);

 

 

 

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi, 

 

If the resource is null, it means there is no such resource. Have you previously loaded the resource while attempting to retrieve it? If so, how did you load the resource '/content/project/en/home/products/p1'?

 

There could be a couple of ways to do this. For example, you could try something like this:

 

//Creating and loading resource
context.build().resource("/content/project/en/home/products/p1").commit();
context.load().json("/my-p1-structure.json","/content/project/en/home/products/p1");

 

 

You can learn more here: https://wcm.io/testing/aem-mock/usage-content-loader-builder.html 


Hope this helps



Esteban Bustamante

View solution in original post

5 Replies

Avatar

Community Advisor

Hi @archana_r 

 

Can you please put a debugger and then check the resourceResolver is not null & the childResource is not null & mocked. And make sure the actual method is passing the same path "/content/project/en/home/products/p1" or you can try with Mockito.anyString() or Mockito.any() instead of declaring the path "/content/project/en/home/products/p1". Like:

when(resourceResolver.getResource(Mockito.anyString())).thenReturn(childResource)

 

Thanks

Avatar

Correct answer by
Community Advisor

Hi, 

 

If the resource is null, it means there is no such resource. Have you previously loaded the resource while attempting to retrieve it? If so, how did you load the resource '/content/project/en/home/products/p1'?

 

There could be a couple of ways to do this. For example, you could try something like this:

 

//Creating and loading resource
context.build().resource("/content/project/en/home/products/p1").commit();
context.load().json("/my-p1-structure.json","/content/project/en/home/products/p1");

 

 

You can learn more here: https://wcm.io/testing/aem-mock/usage-content-loader-builder.html 


Hope this helps



Esteban Bustamante

Avatar

Level 2

Hi Esteban, 

 

Thanks for the pointer. Context.build helped me fix the issue.   Also, how to mock this resource?

Resource res = request.getResource();

 

The below statement helps to pick my mock resource, only when I invoke my model init() method directly.

lenient().when(request.getResource()).thenReturn(resource);

 

When I adapt my resource to model class which in turn calls init, the request objects are returning null.

 

Is there a different way to mock request object when we invoke a method directly or invoke via adaptTo?

Avatar

Community Advisor

Hey, 

 

Glad it worked. Answering your next question, although you could mock getResource(), I think it is better to make sure this returns the "resource" you have loaded through the context.build() method. This is because the request object will be the object from the AEMContext object. You can simply add something like this:

 

context.currentResource("/content/project/en/home/products/p1");


Hope this helps.



Esteban Bustamante

Avatar

Administrator

@archana_r I hope the AEM community has been helpful. We look forward to your return as either a learner or a contributor. The community grows with SMEs like you. Invite your AEM peers to contribute too. Happy AEM learning!

 



Kautuk Sahni