I'm trying to write an integration test using wcm.io Testing AEM Mocks
The code I want to test saves some data in the repository so I would like to verify if the right content is actually stored after I run a tested method.
In my previous tests, I used the JCR_MOCK
Resource Resolver Type, like this:
@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
@Rule
public final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);
private MyClass tested;
@Before
public void setUp() throws Exception {
//here, I load an entire page,
//from which my tested class reads some data
aemContext.load().json("/jcrdata/myPage.json", "/etc/mystuff/myTool");
tested = new MyClass(aemContext.resourceResolver());
}
@Test
public void interactWithTheRepository() {
SomeResult result = tested.interactWithTheRepository();
//assertions here
}
}
This works just fine as long as I just want to read data from the mock repository or pretend I save something. The problem starts when I actually want to verify that some data has been persisted in the repository by the tested class.
@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
@Rule
public final AemContext aemContext = new AemContext(ResourceResolverType.JCR_MOCK);
private MyClass tested;
@Before
public void setUp() throws Exception {
//here, I load an entire page,
//on which the instance of my tested class later saves some data
aemContext.load().json("/jcrdata/myPage.json", "/etc/mystuff/myTool");
tested = new MyClass(aemContext.resourceResolver());
}
@Test
public void storeDataInTheRepository() {
tested.storeDataInTheRepository();
//this comes up as null because the JCR_Mock resolver
//does not use an actual repository and nothing is stored
Resource result = aemContext.resourceResolver().getResource("/etc/mystuff/myTool/somethingSavedByMyClassInstance");
//assertions here
}
}
I believe this can achieved with the JCR_JACKRABBIT
Resource Resolver Type, which uses an actual repository. However, I'm having a hard time loading the data into the mock repository. As soon as I use JCR_JACKRABBIT
instaed of JCR_MOCK
, my setUp
method fails with a NullPointerException
(other parts of the class omitted for clarity)
@Rule
public final AemContext aemContext = new AemContext(ResourceResolverType.JCR_JACKRABBIT);
@Before
public void setUp() throws Exception {
//This line fails with an NPE
aemContext.load().json("/jcrdata/myPage.json", "/etc/mystuff/myTool");
tested = new MyClass(aemContext.resourceResolver());
}
I debugged the code and noticed that it fails while trying to create the resource hierarchy.
java.lang.NullPointerException
at org.apache.sling.resourceresolver.impl.ResourceResolverImpl.create(ResourceResolverImpl.java:1044)
at org.apache.sling.testing.mock.sling.loader.ContentLoader.createResourceHierarchy(ContentLoader.java:183)
at org.apache.sling.testing.mock.sling.loader.ContentLoader.createResourceHierarchy(ContentLoader.java:178)
at org.apache.sling.testing.mock.sling.loader.ContentLoader.json(ContentLoader.java:155)
at org.apache.sling.testing.mock.sling.loader.ContentLoader.json(ContentLoader.java:120)
at com.foo.bar.baz.MyClassTest.setUp(MyClassTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
According to the documentation, Sling Testing should handle the creation of all parent resources while trying to load a page from a JSON document:
Resource org.apache.sling.testing.mock.sling.loader.ContentLoader.json(String classpathResource, String destPath)
Import content of JSON file into repository. Auto-creates parent hierarchies as nt:unstrucured nodes if missing.
However, when the root of the repository is reached and an etc
node is supposed to be created under /
, the ContentLoader
's method createResourceHierarchy
calls the ResourceResolver
with the following parameters:
resourceResolver.create(null, ResourceUtil.getName("/etc"), props);
where props
is a simple HashMap
of the following structure: {jcr:primaryType=nt:unstructured}
, causing a NullPointerException
(due to a null
parent resource being passed)
I'm not sure if this is a bug or something I'm doing wrong, the Sling Testing documentation mentions this about the JCR_JACKRABBIT
Resource Resolver type:
To import Sling content you have to fully register all node types required for the data
but I'm not sure how to interpret this.
I'm using:
io.wcm.testing.aem-mock 1.2.2
org.apache.sling.testing.sling-mock-jackrabbit 0.1.0