Highlighted

AEM project archetype use of it.launcher and it.tests in project structure

sandeepk7656774

29-04-2018

As per reference link, it.launcher and it.tests are related to server side unit tests.  When we are running maven command (mvn clean install -PautoInstallPackage), it just runs the jUnit tests inside core bundle.

I tried to search for logs of AEM, don't find any of log statements from server side unit tests in logs.

What is the use of it.launcher and it.tests folders and when are these tests run on server (Do we need to have some extra configuration for these), these don't seem to be running while installing package on AEM.

Thanks,

Sandeep

Replies

Highlighted

smacdonald2008

30-04-2018

You can test your AEM Java code by using the Test class that is generated for you when you build an Adobe Maven 11+ Archetype project.

For example, assume you create a project named AEM633. Once you build this project, you will have a class named TestHelloWorldModel located in this package:

com.foo.core.models (under src\test\java) 

All of the POM dependencies are setup for you. Create a simple interface in the Core package named Foo.

package com.foo.core;

public interface Foo {

public String getName();

}

Then create an implementation class that uses a @Component annotation in the same package.

package com.foo.core;

import org.osgi.service.component.annotations.Component;

@Component

public class FooImpl implements Foo{

public String getName()

{

  return "Scott" ;

}

}

This is about the most basic AEM class that you can build. It has one method that returns a static value for the getName method. Now the question is how can we test this method?

The answer is to use the TestHelloWorldModel class that already exists. You can create a Foo object and then test the getName method. See the bold code below.

package com.foo.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;

import com.foo.core.Foo;

import com.foo.core.FooImpl;

/**

* Simple JUnit test verifying the HelloWorldModel

*/

public class TestHelloWorldModel {

    //@Inject

    private HelloWorldModel hello;

   

    private Foo myFoo; 

   

    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();

       

        myFoo = new FooImpl();

       

    }

   

    @Test

    public void testGetMessage() throws Exception {

        // some very basic junit tests

        String msg = hello.getMessage();

        assertNotNull(msg);

        assertTrue(msg.length() > 0);

     

     

    }

   

    @Test

    public void testFooMessage() throws Exception {

     

        //custom service

        String msgFoo = myFoo.getName() ;

        assertNotNull(msgFoo);

        assertTrue(msgFoo.length() > 0);

    }

}

When you build the Maven project (for example, mvn clean install), you can see the Test results that are written to this file:

C:\AdobeCQ\AEM633\core\target\surefire-reports\com.foo.core.models.TestHelloWorldModel.txt

Highlighted

sandeepk7656774

30-04-2018

Hello smacdonald2008​,

Thank you for your response. The way you shared is one way to test the Model classes (inside core folder). it.launcher and it.tests folders are still not clear, what is the use in project structure and when they are run.

Regards,

Sandeep