Expand my Community achievements bar.

SOLVED

unable to cover junit for Postconstruct method

Avatar

Level 2

Hi Team,

 

I am trying to create junit for the following postconstruct:

 @PostConstruct
    protected void init() {
        validate();
        if (isValid()) {
            prepareActionsList();
            if(isBlank(type) && isNotBlank(theme)) {
                type= theme.equals("cmp-section-container") ? "full" : "half";
            }
        }
    }

    private void prepareActionsList() {
        if (null != actionsList) {
            actionsList = actionsList.stream().filter(LinkModel::isValid).collect(Collectors.toList());
        }
    }
Here is the junit case trying to address the same:
 
package com.aem.core.models;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.lenient;

import java.util.List;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.factory.ModelFactory;
import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;

@ExtendWith({ AemContextExtension.class, MockitoExtension.class })
public class TextWithCtaModelTest {
    final AemContext ctx = new AemContext();
    @Mock
    private ModelFactory modelFactory;

    @Mock
    private List<LinkModel> actionsList;

    // TextWithCtaModel textWithCtaModel;
    @BeforeEach
    void setUp() throws Exception {

        ctx.addModelsForClasses(TextWithCtaModel.class);
        ctx.load().json("/com/firstdata/aem/core/models/TextWithCtaModelTest.json", "/content");
        lenient().when(modelFactory.getModelFromWrappedRequest(eq(ctx.request()), any(Resource.class), eq(List.class)))
                .thenReturn(actionsList);
    }

    @Test
    void testGetContentView() {
        Resource myResource = ctx.resourceResolver().getResource("/content/textwithcta");
        TextWithCtaModel textWithCtaModel = ctx.getService(ModelFactory.class).createModel(myResource,
                TextWithCtaModel.class);
        final String expected = "Testing Content";
        final String actual = textWithCtaModel.getContentView();
        assertEquals(expected, actual);
    }

    @Test
    void testValidate() {

    }
   
}
 
but it is failing with the following error:
org.apache.sling.models.factory.PostConstructException: Post-construct method has thrown an exception for model class core.models.TextWithCtaModel
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:549)
at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:306)
at org.apache.sling.models.impl.ModelAdapterFactory.createModel(ModelAdapterFactory.java:200)
at com.aem.core.models.TextWithCtaModelTest.testGetContentView(TextWithCtaModelTest.java:44)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at java.util.ArrayList.forEach(ArrayList.java:1257)
Caused by: java.lang.NoSuchMethodError: org.apache.commons.lang3.StringUtils.isAllBlank([Ljava/lang/CharSequence;)Z
at .aem.core.models.TextWithCtaModel.validate(TextWithCtaModel.java:27)
at em.core.models.ContentSectionModel.init(ContentSectionModel.java:73)
at org.apache.sling.models.impl.ModelAdapterFactory.invokePostConstruct(ModelAdapterFactory.java:734)
at org.apache.sling.models.impl.ModelAdapterFactory.createObject
1 Accepted Solution

Avatar

Correct answer by
Level 5

Refer the below article explains the JUNIT writing for a model class.

https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples

 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 5

Refer the below article explains the JUNIT writing for a model class.

https://sourcedcode.com/blog/aem/aem-sling-models-unit-test-junit-4-with-examples