Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

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

Avatar

Level 2

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

 

Avatar

Level 2

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 2

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;