When com.day.cq.wcm.api.designer.Style injected in a Model, the adaptTo returns null
When I inject AEM object Style like this in my Model.
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 ?
Thanks a lot @Stefan.
I think this is what I needed, I'm happy to close this issue.
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).
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.
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, 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:
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 and making sure all methods used by it are covered by the mock provided by Mockito.
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.
So my real question is, how can I use @AemObject along with AemContext as unit test.