[JIRA] (WTES-16) When com.day.cq.wcm.api.designer.Style injected in a Model, the adaptTo returns null

215 views
Skip to first unread message

Lei Zeng (JIRA)

unread,
Mar 2, 2016, 11:11:01 PM3/2/16
to wcm-i...@googlegroups.com
Lei Zeng created WTES-16:
----------------------------

Summary: When com.day.cq.wcm.api.designer.Style injected in a Model, the adaptTo returns null
Key: WTES-16
URL: https://wcm-io.atlassian.net/browse/WTES-16
Project: wcm.io Testing
Issue Type: Bug
Components: AEM Mocks, wcm.io Mocks
Reporter: Lei Zeng


When I inject AEM object Style like this in my Model.
{code:java}
@AemObject
private Style currentStyle;
{code}

I got `java.lang.NullPointerException` that the sling model instance where adaptTo returned is null.

Do we have any solution to mock the Style or fix it inside AemContext ?



--
This message was sent by Atlassian JIRA
(v7.2.0-OD-03-010#72000)

Lei Zeng (JIRA)

unread,
Mar 2, 2016, 11:14:01 PM3/2/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lei Zeng updated WTES-16:
-------------------------
Description:
When I inject AEM object Style like this in my Model.
{code:java}

@Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
public class MyModel {
@AemObject
private Style currentStyle;
}

{code}

I got `java.lang.NullPointerException` that the sling model instance where adaptTo returned is null.

{code:java}

resource = context.create().resource("/content/test/jcr:content/nav");
context.currentPage("/content/test");
context.currentResource(resource);

context.addModelsForPackage("com.my.models");

myModel = resource.adaptTo(MyModel.class);
{code}


Do we have any solution to mock the Style or fix it inside AemContext ?

was:
When I inject AEM object Style like this in my Model.
{code:java}
@AemObject
private Style currentStyle;
{code}

I got `java.lang.NullPointerException` that the sling model instance where adaptTo returned is null.

Do we have any solution to mock the Style or fix it inside AemContext ?


> When com.day.cq.wcm.api.designer.Style injected in a Model, the adaptTo returns null
> ------------------------------------------------------------------------------------
>
> Key: WTES-16
> URL: https://wcm-io.atlassian.net/browse/WTES-16
> Project: wcm.io Testing
> Issue Type: Bug
> Components: AEM Mocks, wcm.io Mocks
> Reporter: Lei Zeng
>
> When I inject AEM object Style like this in my Model.
> {code:java}
> @Model(adaptables = { SlingHttpServletRequest.class, Resource.class })
> public class MyModel {
> @AemObject
> private Style currentStyle;
> }
> {code}
> I got `java.lang.NullPointerException` that the sling model instance where adaptTo returned is null.
> {code:java}
> resource = context.create().resource("/content/test/jcr:content/nav");
> context.currentPage("/content/test");
> context.currentResource(resource);
> context.addModelsForPackage("com.my.models");
> myModel = resource.adaptTo(MyModel.class);
> {code}

Lei Zeng (JIRA)

unread,
Mar 2, 2016, 11:15:01 PM3/2/16
to wcm-i...@googlegroups.com

Stefan Seifert (JIRA)

unread,
Mar 3, 2016, 3:17:03 AM3/3/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13100#comment-13100 ]

Stefan Seifert commented on WTES-16:
------------------------------------

Style API class is currently not supported out-of-the-box in AEM mocks, see http://wcm.io/testing/aem-mock/ for supported features.
currently only a mock implementation for "Designer" is included (without any functionality), but not for Design and Style.

what do you expect from the mocks here? do you expect a real implementation comparable to AEM where within the unit tests a design pages is looked up from the page hierarchy, cells get evaluated and the proper design for the component is detected (if it was prepare before in the mock repository of the unit test)? or is it ok to just get an empty mock Style object that at least the injection works without further caring what it contains?

Lei Zeng (JIRA)

unread,
Mar 3, 2016, 9:32:03 PM3/3/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13101#comment-13101 ]

Lei Zeng commented on WTES-16:
------------------------------

Thanks @Stefan, I have tried to mock the whole Style object.
But it only works when I use `@Inject Style currentStyle`, if I use `@AemObject Style currentStyle`, the `myModel` instance becomes null, with no useful error message.

Lei Zeng (JIRA)

unread,
Mar 3, 2016, 9:58:00 PM3/3/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13101#comment-13101 ]

Lei Zeng edited comment on WTES-16 at 3/4/16 3:57 AM:
------------------------------------------------------

Thanks @Stefan, I have tried to mock the whole Style object.
But it only works when I use `@Inject Style currentStyle`, if I use `@AemObject Style currentStyle`, the `myModel` instance becomes null, with no useful error message.

So my real question is, how can I use @AemObject along with AemContext as unit test.


was (Author: zengleizzz):

Lei Zeng (JIRA)

unread,
Mar 3, 2016, 10:13:01 PM3/3/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13101#comment-13101 ]

Lei Zeng edited comment on WTES-16 at 3/4/16 4:12 AM:
------------------------------------------------------

Thanks @Stefan, I have tried to mock the whole Style object.
But it only works when I use `@Inject Style currentStyle`, if I use `@AemObject Style currentStyle`, the `myModel` instance becomes null, with no useful error message. Even if I put an `@Optional` to it.

Stefan Seifert (JIRA)

unread,
Mar 11, 2016, 8:22:02 AM3/11/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13300#comment-13300 ]

Stefan Seifert commented on WTES-16:
------------------------------------

the problem is not the @AemObject inject, the problem is the lack of support for Design and Style in AEM mocks
i started playing around implementing full support for Designer, Design and Style objects in AEM mocks in this [branch|https://github.com/wcm-io/wcm-io-testing/commits/feature/WTES-16-style-support], but this is quite complicated for a full implementation and still complicated to be used in unit tests.

so i've another proposal: you can register your own adapter factory on-the-fly. i've not tested it, but basically it should work like this:
# create a mock instance of {{com.day.cq.wcm.api.designer.Designer}} e.g. with Mockito
# define behavior of this Designer mock for designer.getDesign(<Page>) and return a mocked {{com.day.cq.wcm.api.designer.Design}} object
# define behavior of this Design mock for design.getStyle(<Cell>) and return a mocked {{com.day.cq.wcm.api.designer.Style}}
# register the mocked Designer instance in your unit test adadaption from ResourceResolver like this:
{code:java}
context.registerAdapter(ResourceResover.class, Designer.class, <Mocked Designer instance>);
{code}

in this case your model should get the mocked style object injected instead of null. this code bypassed the MockDesigner implementation includes in AEM mocks (which does not provide any design or style instances) and uses your mock instead.
i came up with this proposal by looking at the source of [AemObjectInjector|https://github.com/wcm-io/wcm-io-sling/blob/develop/models/src/main/java/io/wcm/sling/models/injectors/impl/AemObjectInjector.java] and making sure all methods used by it are covered by the mock provided by Mockito.
(v7.2.0-OD-03-014#72000)

Lei Zeng (JIRA)

unread,
Mar 14, 2016, 12:52:01 AM3/14/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13400#comment-13400 ]

Lei Zeng commented on WTES-16:
------------------------------

Thanks @Stefen.

Your solution works well!

But I find another way by chance, let me know if it's a hack or working way:
Before I adapt to the Model, I add attributes to the resource or request.
{code:java}
request.setAttribute("resource", mockResource);
request.setAttribute("currentStyle", mockStyle);
myModel = request.adaptTo(MyModel.class);
{code}

Stefan Seifert (JIRA)

unread,
Mar 15, 2016, 1:15:03 PM3/15/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13401#comment-13401 ]

Stefan Seifert commented on WTES-16:
------------------------------------

i assume this will work only if you use @Inject instead of @AemObject annotation - because @Inject queries all possible injection sources, and one of these is "request attributes". if you use @AemObject this should not work, there is no caching of current style in a request attributes of that i'm aware (but i may be wrong here).

Lei Zeng (JIRA)

unread,
Mar 17, 2016, 10:43:01 PM3/17/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13500#comment-13500 ]

Lei Zeng commented on WTES-16:
------------------------------

Thanks a lot @Stefan.

I think this is what I needed, I'm happy to close this issue.

Stefan Seifert (JIRA)

unread,
Mar 18, 2016, 2:45:02 AM3/18/16
to wcm-i...@googlegroups.com

[ https://wcm-io.atlassian.net/browse/WTES-16?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Stefan Seifert closed WTES-16.
------------------------------
Resolution: Done
Reply all
Reply to author
Forward
0 new messages