AEM Mocks - testing of Sling Models that use Proxy Delagate Pattern

785 views
Skip to first unread message

Basic Danijel

unread,
Feb 26, 2021, 9:49:24 AM2/26/21
to wcm-i...@googlegroups.com
Hi everybody,

I'm trying to write a simple AEM Mocks test that verifies a Sling Model. This Sling Model extends an existing Model by using Proxy Delegate pattern.

Sample code:

@Model(
adaptables = SlingHttpServletRequest.class,
adapters = ConfigModel.class,
resourceType = "someapp/components/baseconfig",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class BaseConfigModel implements ConfigModel {
 @Override
 boolean methodA() {
   // some impl
 }
}

@Model(
adaptables = SlingHttpServletRequest.class,
adapters = ConfigModel.class,
resourceType = "someapp/components/customconfig",
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)

// someapp/components/customconfig extends someapp/components/baseconfig

public class CustomConfigModel implements ConfigModel {
 @Self
 @Via(type = ResourceSuperType.class)
 private ConfigModel configModel;

 public boolean methodA() {
   return configModel.methodA();
  }
}

The problem is that the field CustomConfigModel#configModel is always null. AEM Mocks JUnit test is a standard test that loads sample content defined in JSON file.

Could you please give me some help regarding this? I've been looking at io.wcm.wcm.core.components.impl.models.v1.ButtonImplTest but cannot figure out what's needed in order to fix my test.

Best regards,
Danijel

Stefan Seifert

unread,
Feb 26, 2021, 12:56:43 PM2/26/21
to wcm-i...@googlegroups.com
hello danijel.

yes, the "secret" that allows this to work is quite simple, but probably not that obvious.
the sling model implementation of the delegation pattern with "@Self @Via(type = ResourceSuperType.class)" works roughly this way:
- lookup the component definition (resource type) of the current component
- look for a sling:resourceSuperType property
- take this resource type and check if there is a "super model" registered to this resource type and inject it
- or go further up the resource super type hierarchy

in unit tests, there is usually no component definition in the mocked repository, so this fails early. in the test you mentioned this line [1][2] is the glue code that gets it working. however, this works only if you use Sling-Initial-Content style OSGi bundle having the component definitions (resource type) within the same project as JSON files that can be easily mounted in the unit tests.

if you are using a traditional filevault XML layout to describe your components, you can create a minimal skeleton of the resource type in your mocked repository to make it working, like:

context.create().resource("/apps/myapp/components/mycomponent"
"sling:resourceSuperType", "core/wcm/components/button/v1/button");

stefan


[1] https://github.com/wcm-io/wcm-io-wcm-core-components/blob/bd5e773622b576b88292ebb626e62ec510263603/bundles/core/src/test/java/io/wcm/wcm/core/components/impl/models/v1/ButtonImplTest.java#L60
[2] https://github.com/wcm-io/wcm-io-wcm-core-components/blob/bd5e773622b576b88292ebb626e62ec510263603/bundles/core/src/test/java/io/wcm/samples/core/testcontext/TestUtils.java#L56
>--
>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 mailto:wcm-io-dev+...@googlegroups.com.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/wcm-io-
>dev/CAHy%3D9eAJBnDfTMkvJAUVpj6RquYVQK9jmWkQ2H8SGjshhKwvOg%40mail.gmail.com?
>utm_medium=email&utm_source=footer.

Basic Danijel

unread,
Mar 1, 2021, 4:46:40 AM3/1/21
to wcm-i...@googlegroups.com
Hi Stefan,

Thank you for your reply.

The 2nd approach that you proposed works for me. I'll discuss with my colleagues to use Sling-Initial-Content style instead.

Have a nice day.

Best regards,
Danijel

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/AS8PR07MB7238ADF339CCC9F478C4EBC3B89D9%40AS8PR07MB7238.eurprd07.prod.outlook.com.
Reply all
Reply to author
Forward
0 new messages