AEM Cloud Service - Sample JUnit Test using AEMContext

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...

Package Install | Github


Test Failure


Solution

1) 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": "<p>DOEST NOT MATCH Experience AEM</p>\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 = "<p>Experience AEM</p>\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());
}
}

No comments:

Post a Comment