Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

AEM Cloud Service - Sample JUnit Test using AEMContext | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

AEM Cloud Service - Sample JUnit Test using AEMContext by Sreekanth Choudry Nalabotu

Abstract

Goal
Create a simple JUnit for testing a Sling Model class. Unit test checks if the model class function returns content in JCR as-is...

Create Sling Model apps.experienceaem.assets.core.models.FreeFormModel

package apps.experienceaem.assets.core.models;

import com.adobe.cq.export.json.ExporterConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;

@Model( adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME,
extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class FreeFormModel {
@ValueMapValue
@Default(values = StringUtils.EMPTY)
private String content;

public String getContent() {
return content;
}
}


2) Add the mock JCR content in eaem-mock-test\core\src\test\resources\com\eaem\assets\models\FreeFormModelTest.json

{
"eaem": {
"free-form": {
"jcr:primaryType": "nt:unstructured",
"jcr:createdBy": "admin",
"jcr:lastModifiedBy": "admin",
"jcr:created": "Wed Apr 13 2022 12:55:28 GMT-0400",
"content": "

DOEST NOT MATCH Experience AEM

\r\n",
"jcr:lastModified": "Wed Apr 13 2022 12:56:28 GMT-0400"
}
}
}


3) Add the test case apps.experienceaem.assets.core.models.FreeFormModelTest

package apps.experienceaem.assets.core.models;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Objects;

@ExtendWith(AemContextExtension.class)
public class FreeFormModelTest {
private static final String FF_CONTENT_PATH = "/content/eaem/free-form";
private static final String JSON_FILE_PATH = "/com/eaem/assets/models/FreeFormModelTest.json";
private static final String EXPECTED_CONTENT = "

Experience AEM

\r\n";

private AemContext aemContext = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
private FreeFormModel ffModel;

@BeforeEach
void setUp() throws Exception {
aemContext.addModelsForClasses(FreeFormModel.class);
aemContext.load().json(JSON_FILE_PATH, "/content");
Resource resource = aemContext.currentResource(FF_CONTENT_PATH);
ffModel = Objects.requireNonNull(resource.adaptTo(FreeFormModel.class));
}

@Test
void testGetContent() {
assertEquals(EXPECTED_CONTENT, Objects.requireNonNull(ffModel).getContent());
}
}

Read Full Blog

AEM Cloud Service - Sample JUnit Test using AEMContext

Q&A

Please use this thread to ask the related questions.

0 Replies