Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.
SOLVED

Maven problem while trying to work on aem Junit testing

Avatar

Former Community Member

Hi,

I am trying to write Junit test cases and also use mockito for the same. My system set up is:

Windows 64bit desktop,16 GB RAM

I am using AEM 6.3, eclipse Luna,Java 1.8.0_73,Maven 3.5.0

I have wrriten a Junit test case for aem code which requires mocking a session to go ahead, i added few dependencies in my pom.xml like

<dependency>

    <groupId>org.apache.sling</groupId>

    <artifactId>org.apache.sling.testing.jcr-mock</artifactId>

    <version>1.1.14</version>

</dependency>

for mocking.

After i build the project i am getting the below error in the pom.xml file.Please find the stack trace as:

C:\Data\BTproject\AEM\CMS\aem-presales\tags\release_1.0\core\src\main\java\com\bt\aem\presales\component

\LocalNavigationSelectionUse.java : Unable to load compiled class:

com.bt.aem.presales.component.LocalNavigationSelectionUse (org.apache.felix:maven-scr-plugin:1.20.0:scr:generate-scr-

scrdescriptor:process-classes) org.apache.maven.plugin.MojoExecutionException: C:\Data\BTproject\AEM\CMS\aem-

presales\tags\release_1.0\core\src\main\java\com\bt\aem\presales\component\LocalNavigationSelectionUse.java :

Unable to load compiled class: com.bt.aem.presales.component.LocalNavigationSelectionUse at

org.apache.felix.scrplugin.mojo.SCRDescriptorMojo.execute(SCRDescriptorMojo.java:249) at

org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134) at

org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:331) at

org.eclipse.m2e.core.internal.embedder.MavenImpl$11.call(MavenImpl.java:1362) at

org.eclipse.m2e.core.internal.embedder.MavenImpl$11.call(MavenImpl.java:1) at

org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:176) at

org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:112) at

org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:1360) at

org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant.build(MojoExecutionBuildParticipant.java:52) at

org.eclipse.m2e.core.internal.builder.MavenBuilderImpl.build(MavenBuilderImpl.java:137) at

org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:172) at

org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:1) at

org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod$1$1.call(MavenBuilder.java:115) at

org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:176) at

org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:112) at

org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod$1.call(MavenBuilder.java:105) at

org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:176) at

org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:151) at

org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:99) at

org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.execute(MavenBuilder.java:86) at

org.eclipse.m2e.core.internal.builder.MavenBuilder.build(MavenBuilder.java:200) at

org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:735) at

org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) at

org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:206) at

org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:246) at

org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:301) at

org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) at

org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:304) at

org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:360) at

org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:383) at

org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:144) at

org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:235) at

org.eclipse.core.internal.jobs.Worker.run(Worker.java:55) Caused by: org.apache.felix.scrplugin.SCRDescriptorException:

Unable to load compiled class: com.bt.aem.presales.component.LocalNavigationSelectionUse at

org.apache.felix.scrplugin.helper.ClassScanner.scanSources(ClassScanner.java:148) at

org.apache.felix.scrplugin.SCRDescriptorGenerator.execute(SCRDescriptorGenerator.java:146) at

org.apache.felix.scrplugin.mojo.SCRDescriptorMojo.execute(SCRDescriptorMojo.java:237) ... 32 more Caused by:

java.lang.ClassNotFoundException: com.bt.aem.presales.component.LocalNavigationSelectionUse at

java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at

java.lang.ClassLoader.loadClass(Unknown Source) at

org.apache.felix.scrplugin.helper.ClassScanner.scanSources(ClassScanner.java:144) ... 34 more

Can anyone please help me to sort this out?

Thanks,

Anjan raj

1 Accepted Solution

Avatar

Correct answer by
Level 10

Did you build your project using Maven Archetype 11. If no - do that and you will get all of the dependencies that you need to build the project.

You will get this sample code

/*

*  Copyright 2015 Adobe Systems Incorporated

*

*  Licensed under the Apache License, Version 2.0 (the "License");

*  you may not use this file except in compliance with the License.

*  You may obtain a copy of the License at

*

*      http://www.apache.org/licenses/LICENSE-2.0

*

*  Unless required by applicable law or agreed to in writing, software

*  distributed under the License is distributed on an "AS IS" BASIS,

*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

*  See the License for the specific language governing permissions and

*  limitations under the License.

*/

package com.foo.community.core.models;

import static org.junit.Assert.assertNotNull;

import static org.junit.Assert.assertTrue;

import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.when;

import java.util.UUID;

import junitx.util.PrivateAccessor;

import org.apache.sling.settings.SlingSettingsService;

import org.junit.Before;

import org.junit.Test;

/**

* Simple JUnit test verifying the HelloWorldModel

*/

public class TestHelloWorldModel {

    //@Inject

    private HelloWorldModel hello;

   

    private String slingId;

   

    @Before

    public void setup() throws Exception {

        SlingSettingsService settings = mock(SlingSettingsService.class);

        slingId = UUID.randomUUID().toString();

        when(settings.getSlingId()).thenReturn(slingId);

        hello = new HelloWorldModel();

        PrivateAccessor.setField(hello, "settings", settings);

        hello.init();

    }

   

    @Test

    public void testGetMessage() throws Exception {

        // some very basic junit tests

        String msg = hello.getMessage();

        assertNotNull(msg);

        assertTrue(msg.length() > 0);

    }

}

View solution in original post

6 Replies

Avatar

Level 10

YOu can see examples under the test package when you create a Maven Archetype 11 project -- see Creating an Adobe Experience Manager 6.3 Project using Adobe Maven Archetype 11

Avatar

Level 8

Hi,


Can you try with the below entries as dependencies in your pom.xml and see if it helps.

<dependency>

            <groupId>org.mockito</groupId>

            <artifactId>mockito-all</artifactId>

            <version>1.10.19</version>

        </dependency>

<dependency>

            <groupId>org.jmockit</groupId>

            <artifactId>jmockit</artifactId>

        </dependency>

Avatar

Former Community Member

Hi Mac,

Thanks for the reply.The code structure is already there and i am trying to write only Junits for a service class.When i started writing the Junit, there is a method call which takes session as a parameter. So i learnt that i need to mock the session in order to go ahead.So i started adding dependencies in the pom.xml. I encountered the above error, and i am trying to resolve it, but it still persists.What can i do next? please suggest.

Thanks,

Anjan

Avatar

Former Community Member

Hi cquser1,

Thanks for the reply. I tried adding the above blocks,but not working yet.Pls suggest.

Thanks,

Anjan

Avatar

Correct answer by
Level 10

Did you build your project using Maven Archetype 11. If no - do that and you will get all of the dependencies that you need to build the project.

You will get this sample code

/*

*  Copyright 2015 Adobe Systems Incorporated

*

*  Licensed under the Apache License, Version 2.0 (the "License");

*  you may not use this file except in compliance with the License.

*  You may obtain a copy of the License at

*

*      http://www.apache.org/licenses/LICENSE-2.0

*

*  Unless required by applicable law or agreed to in writing, software

*  distributed under the License is distributed on an "AS IS" BASIS,

*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

*  See the License for the specific language governing permissions and

*  limitations under the License.

*/

package com.foo.community.core.models;

import static org.junit.Assert.assertNotNull;

import static org.junit.Assert.assertTrue;

import static org.mockito.Mockito.mock;

import static org.mockito.Mockito.when;

import java.util.UUID;

import junitx.util.PrivateAccessor;

import org.apache.sling.settings.SlingSettingsService;

import org.junit.Before;

import org.junit.Test;

/**

* Simple JUnit test verifying the HelloWorldModel

*/

public class TestHelloWorldModel {

    //@Inject

    private HelloWorldModel hello;

   

    private String slingId;

   

    @Before

    public void setup() throws Exception {

        SlingSettingsService settings = mock(SlingSettingsService.class);

        slingId = UUID.randomUUID().toString();

        when(settings.getSlingId()).thenReturn(slingId);

        hello = new HelloWorldModel();

        PrivateAccessor.setField(hello, "settings", settings);

        hello.init();

    }

   

    @Test

    public void testGetMessage() throws Exception {

        // some very basic junit tests

        String msg = hello.getMessage();

        assertNotNull(msg);

        assertTrue(msg.length() > 0);

    }

}

Avatar

Former Community Member

Hi Mac,

Thanks for the reply. I built the project using archetype 11 and a folder got created as shown below.

1333999_pastedImage_0.png

But the code for which i have to write Junit is in the below project code under "aem-presales.core" in which the pom.xml shows some error as i have already posted.

1334004_pastedImage_2.png

The error log does not say that there are dependencies missing,but says some sort of "

org.apache.felix:maven-scr-plugin:1.20.0:scr:generate-scr-

scrdescriptor:process-classes". So do you think its a build issue or something else?

Please suggest how i should go about this?