Integration testing with WCM IO - AemContext | Community
Skip to main content
salvadorm142499
February 24, 2017
Solved

Integration testing with WCM IO - AemContext

  • February 24, 2017
  • 7 replies
  • 7955 views

Hi all,

I am trying to write Junit test by using WCM IO testing tools. I have been reading lots of articles but I can't make it work. The steps I follow are:

  1. Add maven dependency for AEM 6.2
        <dependency>     <groupId>io.wcm</groupId>     <artifactId>io.wcm.testing.aem-mock</artifactId>     <version>2.2.0</version>     </dependency>
  2. Add test case within the core bundle

    package com.my.project.core.models; import org.apache.sling.api.resource.ResourceResolver; import org.junit.Rule; import org.junit.Test; import io.wcm.testing.mock.aem.junit.AemContext; public class MyFirstITest { @Rule public final AemContext context = new AemContext(); @Test public void itWorksTest() throws Exception { ResourceResolver rr =  context.resourceResolver(); // Test someting } }
  3. Run the test --> ERROR: java.lang.NoClassDefFoundError: org/junit/rules/TestRule.... Caused by: java.lang.ClassNotFoundException: org.junit.rules.TestRulea
  4. Building with maven --> ERROR: COMPILATION ERROR : ... MyFirstITest.java:[16,47] cannot access org.junit.rules.TestRule .... class file for org.junit.rules.TestRule not found

 

Does anybody use this library to write unit tests? Does anybody make it work? I have been following: http://wcm.io/testing/aem-mock/usage.html#Sling_Models

 

Cheers,

Salva.

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 Daniel_H__A__Li

Hi, Salvador.

Were you able to make it work?

I'm still testing wcm.io with 6.2 but, for now, the following list of dependencies (pom.xml) has worked for me:

<dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.compendium</artifactId> <version>5.0.0</version> <scope>provided</scope> </dependency> <!-- more dependencies here --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> <dependency> <groupId>junit-addons</groupId> <artifactId>junit-addons</artifactId> <version>1.4</version> <scope>test</scope> </dependency> <dependency> <groupId>io.wcm.maven</groupId> <artifactId>io.wcm.maven.aem-dependencies</artifactId> <version>6.2.0.0000</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.wcm</groupId> <artifactId>io.wcm.testing.aem-mock</artifactId> <version>2.2.0</version> <!-- http://wcm.io/testing/aem-mock/ --> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-imaging</artifactId> <version>1.0-R1534292</version> <scope>test</scope> </dependency> <!-- more dependencies here --> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.6.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.6</version> <scope>test</scope> </dependency> <!-- more dependencies here --> <dependency> <groupId>com.adobe.aem</groupId> <artifactId>uber-jar</artifactId> <version>6.2.0</version> <scope>provided</scope> <classifier>apis</classifier> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.6</version> <scope>test</scope> </dependency> <!-- more dependencies here -->

It came from combining AMC AEM Archetype + ACS AEM Lazybones + a fair amount of trial and error.

Regards,

Daniel.

7 replies

March 1, 2017

Even I am facing the same issue.

Daniel_H__A__Li
March 1, 2017

Hi, Salvador.

For AEM 6.1, we've been using the following combination of test libraries:

<dependency> <groupId>io.wcm.maven</groupId> <artifactId>io.wcm.maven.aem-dependencies</artifactId> <version>6.1.0.0000</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.wcm</groupId> <artifactId>io.wcm.testing.aem-mock</artifactId> <version>1.6.2</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>junit-addons</groupId> <artifactId>junit-addons</artifactId> <version>1.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.5.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.4</version> <scope>test</scope> </dependency>

 

Maybe there is also an io.wcm.maven.aem-dependencies for AEM 6.2...

Regards,

Daniel.

salvadorm142499
March 1, 2017

Hi Daniel,

It helps a lot to resolve the previous issue and now, I can use AemContext Object. However, I am facing the next issue:

        Running wcm.salvaITest.PageTest
        Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.398 sec <<< FAILURE! - in wcm.salvaITest.PageTest
        testSomething(wcm.salvaITest.PageTest)  Time elapsed: 0.322 sec  <<< ERROR!
        java.lang.NoClassDefFoundError: org/apache/sling/models/factory/ModelFactory

All I have done so far is:

  1. Generate AEM project by using Eclipse plugin and maven archetype version 10.
  2. Create PageTest within the module it.tests. See following picture for further information --> 
  3. Run maven clean install -autoInstallPackage

Have you also faced this issue?

Obrigado, Salva.

Daniel_H__A__Li
March 1, 2017

Hi, Salvador.

As a side note:

I think it.tests project is meant for integration tests only: server-side tests that run inside AEM.

I would put unit tests in a different child project/module.

About your question:

It seems that some dependencies are still missing... Does IntelliJ automatically import the dependencies declared in Maven POM files?

Regards,

Daniel.

Daniel_H__A__Li
March 2, 2017

Hi, Salvador.

<dependency> <groupId>org.apache.sling</groupId> <artifactId>org.apache.sling.models.api</artifactId> <version>1.2.2</version> <scope>provided</scope> </dependency>

You can discover which version to use by searching for org.apache.sling.models.factory.ModelFactory at the /system/console/depfinder.

If IntelliJ is not able to automatically import the dependencies declared in your POM files, I would say to you:

  • To move your unit tests to a different subproject/module;
  • To use Maven to run them: mvn clean test;
  • To ask Maven which dependencies are being used for the tests: mvn dependency:tree -Dscope=test

Best regards,

Daniel.

smacdonald2008
March 2, 2017

We have added this use case to the list of HELPX articles to write. All of this information should be in an article. We hope to get this written shortly after Digital Marketing summit. 

Daniel_H__A__Li
Daniel_H__A__LiAccepted solution
March 15, 2017

Hi, Salvador.

Were you able to make it work?

I'm still testing wcm.io with 6.2 but, for now, the following list of dependencies (pom.xml) has worked for me:

<dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.core</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>org.osgi.compendium</artifactId> <version>5.0.0</version> <scope>provided</scope> </dependency> <!-- more dependencies here --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> <scope>test</scope> </dependency> <dependency> <groupId>junit-addons</groupId> <artifactId>junit-addons</artifactId> <version>1.4</version> <scope>test</scope> </dependency> <dependency> <groupId>io.wcm.maven</groupId> <artifactId>io.wcm.maven.aem-dependencies</artifactId> <version>6.2.0.0000</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>io.wcm</groupId> <artifactId>io.wcm.testing.aem-mock</artifactId> <version>2.2.0</version> <!-- http://wcm.io/testing/aem-mock/ --> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-imaging</artifactId> <version>1.0-R1534292</version> <scope>test</scope> </dependency> <!-- more dependencies here --> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.6.6</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.6</version> <scope>test</scope> </dependency> <!-- more dependencies here --> <dependency> <groupId>com.adobe.aem</groupId> <artifactId>uber-jar</artifactId> <version>6.2.0</version> <scope>provided</scope> <classifier>apis</classifier> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.6</version> <scope>test</scope> </dependency> <!-- more dependencies here -->

It came from combining AMC AEM Archetype + ACS AEM Lazybones + a fair amount of trial and error.

Regards,

Daniel.