Skip to main content
Level 2
Mar 7, 2022 3:07 PM UTC
Solved

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

How to mock

@586265

@source(“script-bindings”)

Page currentPage;

and

@586265

@3200416("script-bindings")

private ValueMap pageProperties; 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Anmol_Bhardwaj
Anmol_BhardwajAccepted solution
Community Advisor
Mar 8, 2022 7:32 AM UTC

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%20if%20required).

 

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

 

5 replies

Anmol_Bhardwaj
Community Advisor
Anmol_BhardwajCommunity AdvisorAccepted solution
Community Advisor
Mar 8, 2022 7:32 AM UTC

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%20if%20required).

 

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

 

Translate
Level 2
Mar 9, 2022 5:21 PM UTC

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

Translate
Anmol_Bhardwaj
Community Advisor
Community Advisor
Mar 10, 2022 7:30 AM UTC

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

Translate