NPE when starting a simple test suite with AEM Mocks 2.0 and ResourceResolverType = JCR_OAK

522 views
Skip to first unread message

dpl...@gmail.com

unread,
Sep 16, 2016, 9:12:46 AM9/16/16
to wcm-io Developers
Hello,

I set up a small project to test the AemMocks dependencies (because the "big" project kept failing) and I'm stuck with an NPE:

java.lang.NullPointerException
at org.apache.sling.testing.mock.sling.context.SlingContextImpl.resourceResolver(SlingContextImpl.java:189)

The project is at https://github.com/dplaton/test-aem-mocks - it has just a test class which creates a resource in the "setUp" method.

Stefan Seifert

unread,
Sep 16, 2016, 9:43:27 AM9/16/16
to wcm-i...@googlegroups.com
the major issue: the @Rule annotation is missing on the context field.
you may also need to add the osgi dependencies.

stefan

p.s. the project uses non-public dependencies, makes it difficult to test.

dpl...@gmail.com

unread,
Sep 16, 2016, 10:45:18 AM9/16/16
to wcm-io Developers
Hi Stefan,

You're right, it's that kind of error that makes you go crazy when in fact the cause is trivial - adding the @Rule annotation fixed the NPE, but leads to another error:

- when using JCR_MOCK I get a java.lang.RuntimeException: Unable to initialize JCR_MOCK resource resolver factory: Unable to get ResourceResolverFactory.

- when using JCR_OAK I get "java.lang.RuntimeException: Unable to initialize JCR_OAK resource resolver factory: Unable to invoke method 'activate' for class org.apache.sling.testing.mock.sling.oak.OakMockSlingRepository", caused by Caused by: java.lang.IllegalAccessError: tried to access class org.apache.jackrabbit.oak.plugins.version.VersionEditorProvider from class org.apache.jackrabbit.oak.jcr.Jcr

I updated my project to use public dependencies.

Thank you and have a nice day,
Dan

Stefan Seifert

unread,
Sep 16, 2016, 11:29:44 AM9/16/16
to wcm-i...@googlegroups.com
now you have a mismatch of versions. you use AEM API 6.0 which is quite old, but very new version of jcr.resource and sling-mock, this is incompatible.

if you want to go the AEM 6.0 way change your pom like this [1] and use AEM mocks 1.x

if you want to go AEM 6.2 use [2], with aem mocks 2.x. use 2.0.1-SNAPSHOT because there was some mess in the dependencies in version 2.0.0 (new 2.0 version will be released soon).

and i strongly recommend not to use org.apache.sling.commons.testing when you are using aem-mocks or sling-mocks - it brings a lot of transitive dependencies in the project that are quite old. aem-mocks/sling-mocks in the major version range 1.x or 2.x should give a good start with the correct dependencies for AEM 6.0/6.1 (1.x) or AEM 6.2 (2.x)

stefan


[1]

@@ -42,16 +42,6 @@
<version>5.0.0</version>
</dependency>
<dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.commons.osgi</artifactId>
- <version>2.4.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.sling</groupId>
- <artifactId>org.apache.sling.jcr.resource</artifactId>
- <version>2.8.0</version>
- </dependency>
- <dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.18</version>
@@ -76,7 +66,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock</artifactId>
- <version>2.0.0</version>
+ <version>1.8.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>


[2]

@@ -64,8 +64,9 @@
</dependency>
<dependency>
<groupId>com.adobe.aem</groupId>
- <artifactId>aem-api</artifactId>
- <version>6.0.0.1</version>
+ <artifactId>uber-jar</artifactId>
+ <version>6.2.0</version>
+ <classifier>apis</classifier>
<scope>provided</scope>
</dependency>
<dependency>
@@ -76,7 +77,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock</artifactId>
- <version>2.0.0</version>
+ <version>2.0.1-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
@@ -87,14 +88,16 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.sling-mock-oak</artifactId>
- <version>1.0.0</version>
+ <version>2.0.0</version>
</dependency>
</dependencies>

</project>

dpl...@gmail.com

unread,
Sep 17, 2016, 7:02:20 AM9/17/16
to wcm-io Developers

Hi Stefan,

Well, I finally got it working, using the aem-mocks 2.0.1-SNAPSHOT.
I was using org.apache.sling.commons.testing library in order to use RepositoryUtil#registerNodeTypes(). Do I have an alternative for registering node-types other that o.a.s.c.testing?

Thank you and have a nice day,
Dan

Stefan Seifert

unread,
Sep 19, 2016, 9:57:21 AM9/19/16
to wcm-i...@googlegroups.com
for most use cases you do not need to register the node types manually.

first of all, you need this only for the resource resolver types JCR_OAK and JCR_JACKRABBIT. the other resource resolver types (RESOURCERESOLVER_MOCK, JCR_MOCK) do not care about node types and if they are registered or not.

if you use JCR_OAK or JCR_JACKRABBIT the node types are registered automatically for all node type definitions that are registered in a MANIFEST.MF via a Sling-Nodetypes statement (and if the node type definition it points to is found in the class path as well). this is documented (very briefly) at [1]. the code behind this is [2].

if you want to do it manually you can use the JCR API directly e.g. using org.apache.jackrabbit.commons.cnd.CndImporter.

stefan

[1] http://sling.apache.org/documentation/development/sling-mock.html
[2] https://svn.apache.org/repos/asf/sling/trunk/testing/mocks/sling-mock/src/main/java/org/apache/sling/testing/mock/sling/NodeTypeDefinitionScanner.java


>-----Original Message-----
>From: wcm-i...@googlegroups.com [mailto:wcm-i...@googlegroups.com] On
>Behalf Of dpl...@gmail.com
>Sent: Saturday, September 17, 2016 1:02 PM
>To: wcm-io Developers
>Subject: Re: NPE when starting a simple test suite with AEM Mocks 2.0 and
>ResourceResolverType = JCR_OAK
>
>
>Hi Stefan,
>
Reply all
Reply to author
Forward
0 new messages