Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Mocking @Inject Page currentPage; and @Inject @Source("script-bindings") private ValueMap pageProperties is giving null

Avatar

Level 3

How to mock

@inject

@source(“script-bindings”)

Page currentPage;

and

@inject

@source("script-bindings")

private ValueMap pageProperties; 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @kanikaa32870678 ,

 

If you're using AEMContext, it would be pretty straight forward. Try this:

 Page page = context.currentPage(CONTAINING_PAGE);
 slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());

You can mock value map similarly.

 

https://wcm.io/testing/aem-mock/usage.html#:~:text=The%20AEM%20mock%20context%20can,in%20parallel%2C....

 

AemContext : https://wcm.io/testing/aem-mock/junit5/apidocs/io/wcm/testing/mock/aem/junit5/AemContext.html

 

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi @kanikaa32870678 ,

 

If you're using AEMContext, it would be pretty straight forward. Try this:

 Page page = context.currentPage(CONTAINING_PAGE);
 slingBindings = (SlingBindings) context.request().getAttribute(SlingBindings.class.getName());

You can mock value map similarly.

 

https://wcm.io/testing/aem-mock/usage.html#:~:text=The%20AEM%20mock%20context%20can,in%20parallel%2C....

 

AemContext : https://wcm.io/testing/aem-mock/junit5/apidocs/io/wcm/testing/mock/aem/junit5/AemContext.html

 

I tried that but not working. For currentPage @inject and @source(“script-bindings”) both are used in the sling model. 

Avatar

Community Advisor

That's fine, what you need to do is, when you create test class for sling modal, use AemContext and get the injected properties through the way described above

Avatar

Level 3

I have used AEM mock context resourceResolver type as JCR_MOCK then currentStyle and CurrentPage is started working. 

Still the issue is with 

@inject
@source("script-bindings")
private ValueMap pageProperties;