Adapt resource to Page int unit test | Community
Skip to main content
Level 2
July 5, 2018
Solved

Adapt resource to Page int unit test

  • July 5, 2018
  • 5 replies
  • 7233 views

Hi everyone, I am having problems to test Pages in my unit tests. I have the following code:

Resource resource = context.load().json(PAGE_PATH + JSON_EXT, PAGE_PATH);

Page page = resource.adaptTo(Page.class);

The answer of adapTo is always null.

I am working with SlingMocks, and this is my resource

public final SlingContext context = new SlingContext();

There is a option to do this? I know its possible with wcm.io​ but I don't want to include that just for this kind of tests.

Thanks in advance.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Peter_Puzanovs

Dear Jora,

Sling does not know about AEM WCM Page Api, therefore, it won't handle it OOTB. Of course you can wring your custom logic to Sling Mock's it how to handle Page API, but then you are re-implementing wcm.io.


Regards,Peter

5 replies

Peter_Puzanovs
Community Advisor
Community Advisor
July 5, 2018

Dear Jora,

Have you looked at [1]

[1] Usage | AEM Mocks :

public class ExampleTest {

  @Rule

  public final AemContext context = new AemContext();

  @Test

  public void testSomething() {

  Resource resource = context.resourceResolver().getResource("/content/sample/en");

  Page page = resource.adaptTo(Page.class);

  // further testing

  }

}

shows basic dummy example [2]

[2] GitHub - xuanuy/mock-testing

Regards,

Peter

jorarteagAuthor
Level 2
July 6, 2018

Thanks for the answer Peter.

Yes, I already saw that, but I'm wondering if this is possible without wcm.io (that's where it comes from AemContext), now I just use Apache Sling Mocks. I'm looking for solutions using just this dependency, and if is not possible I will jump to the other one.

Peter_Puzanovs
Community Advisor
Peter_PuzanovsCommunity AdvisorAccepted solution
Community Advisor
July 6, 2018

Dear Jora,

Sling does not know about AEM WCM Page Api, therefore, it won't handle it OOTB. Of course you can wring your custom logic to Sling Mock's it how to handle Page API, but then you are re-implementing wcm.io.


Regards,Peter

Peter_Puzanovs
Community Advisor
Community Advisor
July 6, 2018

There should not be too much risk(check with your penetration security team before adding any dependency) adding wcm.io, if you declare it as <scope>test</scope> time dependency in maven. It's only initialised for testing then.

Regards,

Peter

jorarteagAuthor
Level 2
July 6, 2018

Thanks Peter! I will look whats its the more convenient solution for us.