Expand my Community achievements bar.

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

5 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;

Avatar

Level 1

Did you find a way to get the pageProperties working? I'm not able to get anything but null. I verified and i'm adding a proper valuemap in the sling bindings object but still not picking it