[SLING MOCK] Set RequestContext automatically

143 views
Skip to first unread message

Alex Muthmann

unread,
Dec 19, 2016, 3:31:59 PM12/19/16
to wcm-i...@googlegroups.com
Hi,

during one of our tests we came to a point, where we wanted to adapt a
component that is using "wcmmode" and is adaptable from a Resource and a
SlingHttpRequest. While the later one works fine in our unit test, it
was not possible to adapt the component from a Resource as the
AEMObjectInjector needs a Request to adapt to WCMMode (and other
classes):
https://github.com/wcm-io/wcm-io-sling/blob/develop/models/src/main/java/io/wcm/sling/models/injectors/impl/AemObjectInjector.java#L104

We found the solution in MockSlingExtensions:
MockSlingExtensions.setRequestContext(context, context.request());

My proposal:

I can't really see a situation, where I would use the AemContext and a
set request context does any harm. So I think this pitfall could be
removed by automatically setting the RequestContext.

Cheers


Stefan Seifert

unread,
Dec 19, 2016, 3:49:30 PM12/19/16
to wcm-i...@googlegroups.com
well, first of all aem-mock does not know anything about wcm.io sling extensions because it's a separate module not necessarily present in every project.

then there are usecases where you want to test with our without request context: if you have sling models that should be usable both within and without request context you have to test both variants. so you need control whether a request context is set or not.

if you always want to set the request context but this code in a central class you use to set up the aem context for all unit tests.

stefan

Alex Muthmann

unread,
Dec 20, 2016, 3:09:49 AM12/20/16
to wcm-i...@googlegroups.com
ok, fair enough. I'll add a short notice to the documentation so it
becomes more clear.
https://github.com/wcm-io/wcm-io-testing/pull/3

Stefan Seifert

unread,
Dec 20, 2016, 6:38:09 AM12/20/16
to wcm-i...@googlegroups.com
thanks, i've merged it with slight modifications
>--
>You received this message because you are subscribed to the Google Groups
>"wcm-io Developers" group.
>To unsubscribe from this group and stop receiving emails from it, send an
>email to wcm-io-dev+...@googlegroups.com.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/wcm-io-
>dev/4197b6abd7fa7eb19e05d9ac8dc74947%40dev-eth0.de.
>For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages