Expand my Community achievements bar.

Error while Using Sling Mock with JCR_OAK

Avatar

Level 1

I'm building some tests and I'm finding an issue while trying to create a new test with JCR_OAK

[main] INFO org.apache.jackrabbit.oak.plugins.index.IndexUpdate - Found a new index node [reference]. Reindexing is requested

[main] INFO org.apache.jackrabbit.oak.plugins.index.IndexUpdate - Reindexing will be performed for following indexes: [/oak:index/slingResourceType, /oak:index/slingAlias, /oak:index/uuid, /oak:index/slingVanityPath, /oak:index/slingResource, /oak:index/jcrLanguage, /oak:index/reference, /oak:index/jcrLockOwner, /oak:index/nodetype]

[main] INFO org.apache.jackrabbit.oak.plugins.index.IndexUpdate - Indexing report

    - /oak:index/nodetype*(1238)

[main] INFO org.apache.jackrabbit.oak.plugins.index.IndexUpdate - Reindexing will be performed for following indexes: [/oak:index/principalName, /oak:index/authorizableId, /oak:index/acPrincipalName]

[main] INFO org.apache.jackrabbit.oak.plugins.index.IndexUpdate - Indexing report

    - /oak:index/principalName*(2)

    - /oak:index/authorizableId*(2)

java.lang.RuntimeException: Unable to initialize JCR_OAK resource resolver factory: class sun.security.provider.ConfigFile

at org.apache.sling.testing.mock.sling.context.ContextResourceResolverFactory.get(ContextResourceResolverFactory.java:66)

at org.apache.sling.testing.mock.sling.context.SlingContextImpl.newResourceResolverFactory(SlingContextImpl.java:129)

at org.apache.sling.testing.mock.sling.context.SlingContextImpl.resourceResolverFactory(SlingContextImpl.java:134)

at org.apache.sling.testing.mock.sling.context.SlingContextImpl.resourceResolver(SlingContextImpl.java:231)

at org.apache.sling.testing.mock.sling.context.SlingContextImpl.create(SlingContextImpl.java:318)

at com.clientname.commerce.clientnameProductImporterTest.testImport(clientnameProductImporterTest.java:58)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:498)

at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)

at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)

at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$LastRuleTestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:148)

at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:91)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)

at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)

at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)

at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)

at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)

at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)

at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)

at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)

at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)

at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)

at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)

at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)

at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: java.lang.ClassCastException: class sun.security.provider.ConfigFile

at java.lang.Class.asSubclass(Class.java:3404)

at javax.security.auth.login.Configuration$2.run(Configuration.java:254)

at javax.security.auth.login.Configuration$2.run(Configuration.java:247)

at java.security.AccessController.doPrivileged(Native Method)

at javax.security.auth.login.Configuration.getConfiguration(Configuration.java:246)

at org.apache.jackrabbit.oak.security.authentication.LoginContextProviderImpl.getConfiguration(LoginContextProviderImpl.java:137)

at org.apache.jackrabbit.oak.security.authentication.LoginContextProviderImpl.getLoginContext(LoginContextProviderImpl.java:85)

at org.apache.jackrabbit.oak.core.ContentRepositoryImpl.login(ContentRepositoryImpl.java:164)

at org.apache.jackrabbit.oak.jcr.repository.RepositoryImpl.login(RepositoryImpl.java:280)

at org.apache.jackrabbit.oak.jcr.repository.RepositoryImpl.login(RepositoryImpl.java:219)

at org.apache.sling.testing.mock.sling.oak.OakMockSlingRepository.login(OakMockSlingRepository.java:125)

at org.apache.sling.testing.mock.sling.oak.OakMockSlingRepository.loginAdministrative(OakMockSlingRepository.java:141)

at org.apache.sling.testing.mock.sling.ResourceResolverFactoryInitializer.registerJcrNodeTypes(ResourceResolverFactoryInitializer.java:179)

at org.apache.sling.testing.mock.sling.ResourceResolverFactoryInitializer.setUp(ResourceResolverFactoryInitializer.java:64)

at org.apache.sling.testing.mock.sling.MockSling.newResourceResolverFactory(MockSling.java:75)

at org.apache.sling.testing.mock.sling.context.ContextResourceResolverFactory.get(ContextResourceResolverFactory.java:45)

... 36 more

This is my test setup:

package com.clientname.commerce;

import static org.mockito.Mockito.*;

import javax.jcr.Node;

import javax.jcr.Session;

import java.io.InputStream;

import java.util.Random;

import com.day.cq.dam.api.Asset;

import com.day.cq.dam.api.Rendition;

import com.clientname.utils.Constants;

import com.clientname.utils.Utility;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.testing.mock.sling.MockSling;

import org.apache.sling.testing.mock.sling.ResourceResolverType;

import org.apache.sling.testing.mock.sling.junit.SlingContext;

import org.junit.Before;

import org.junit.Rule;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.mockito.runners.MockitoJUnitRunner;

import org.powermock.api.mockito.PowerMockito;

import org.powermock.core.classloader.annotations.PrepareForTest;

import org.powermock.modules.junit4.PowerMockRunner;

/**

* Tests for the import process.

*/

@RunWith(PowerMockRunner.class)

@PrepareForTest(Utility.class)

public class ClientnameProductImporterTest {

  private static final String PATH = "/content/dam/somepath";

  @Rule

  public final SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK);

  private ClientnameProductImporter clientnameProductImporter;

  @Before

  public void setUp() throws Exception {

    clientnameProductImporter = new ClientnameProductImporter();

    PowerMockito.mockStatic(Utility.class);

    PowerMockito.when(Utility.getReplicator()).thenReturn(null);

  }

  @Test

  public void testImport() throws Exception {

    ResourceResolver resourceResolver = MockSling.newResourceResolver(context.bundleContext());

    Node jsonStoreRoot = mock(Node.class);

    when(jsonStoreRoot.getPath()).thenReturn(PATH);

    context.create().resource(PATH,"propertyName", "propertyValue");

    clientnameProductImporter.doImport(resourceResolver,

            jsonStoreRoot,

            (new Random()).nextBoolean()

    );

  }

}

I've not been able to get JCR_MOCK to work at all. The error is when the context.create() method is called.

Also while reading through this: http://sling.apache.org/documentation/development/sling-mock.html

Apache Sling :: Sling Mocks

It says:

  • Node types defined in OSGi bundle header 'Sling-Nodetypes' found in MANIFEST.MF files in the classpath are registered automatically.

Looking for an example on how to add these.

Any ideas on how to resolve this issue? it says Caused by: java.lang.ClassCastException: class sun.security.provider.ConfigFile but not sure how to proceed.

I've been looking through the code on github but haven't been able to figure it out GitHub - apache/sling-org-apache-sling-testing-sling-mock: Apache Sling Testing Sling Mock

1 Reply

Avatar

Level 1

What AEM Version are you working with and which test related dependencies did you add to the POM?