Expand my Community achievements bar.

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

Avatar

Level 4

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

6 Replies

Avatar

Level 10

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

Avatar

Level 4

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

Avatar

Level 1

Hi! I'm leaving some info for any who need answers to these AEM modules (mainly to explain ui.launcher & ui.tests better)


Screenshot 2022-07-21 131737.png


Cheers!

Avatar

Level 2
  • it.launcher: contains glue code that deploys the ui.tests bundle (and dependent bundles) to the server and triggers the remote JUnit execution

https://github.com/Adobe-Marketing-Cloud/aem-sample-we-retail/blob/master/README.md

 

This has some details around the it.launcher module.

Avatar

Level 1

It servers the purpose of tests that are executed on a running AEM instance, as opposed to Unit Tests with Mockito (or another framework) in a mock AEM context. e.g. https://github.com/sneakybeaky/AEM-Integration-Test-Example