0

Since i am new to Mockito and AEM model java. I have a gone through some docs and wrote my first Mockito file for AEM Model java. In my code i've not see any errors, but while running i am not getting success and can't complete the code coverage 100%. Can anyone correct/help me to fix my code[given sample java with respective mockito file]

Java File:

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;
import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.abc.cc.ddd.ResourceResolverService;
import com.abc.cc.ddd.services.models.bean.Accordion;
@Model(adaptables = SlingHttpServletRequest.class)
public class AccordionModel {
    private final static Logger log = LoggerFactory.getLogger(AccordionModel.class);
    @SlingObject
    private SlingHttpServletRequest request;
    @Inject
    public ResourceResolverService resolverService;
    private Resource resource;
    public List < Accordion > accordionList = new ArrayList < Accordion > ();
    @PostConstruct
    protected void init() throws LoginException, JSONException {
        log.info("AccordionModel init method Start");
        resource = request.getResource();
        final ValueMap configurationOptionProperties = resource.getValueMap();
        log.debug("iconfigurationOptionProperties is " + configurationOptionProperties);
        String count = configurationOptionProperties.get("count", String.class);
        if (count != null) {
            for (int i = 1; i <= Integer.valueOf(count); i++) {
                Accordion accordion = new Accordion();
                String title = configurationOptionProperties.get("title" + i, String.class);
                String rte = configurationOptionProperties.get("rte" + i, String.class);
                accordion.setTitle(title);
                accordion.setRte(rte);
                accordionList.add(accordion);
            }
        }
        log.info("AccordionModel init method End");
    }
    public List < Accordion > getAccordionList() {
        return accordionList;
    }
}

Mockito code

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.abc.cc.ddd.ResourceResolverService;
import com.abc.cc.ddd.services.models.bean.Accordion;
@RunWith(MockitoJUnitRunner.class)
public class AccordionModelTest {
    @InjectMocks
    private AccordionModel accordionModel;
    @Mock
    Resource resource;
    @Mock
    SlingHttpServletRequest request;
    @Mock
    ResourceResolverService resourceResolverService;
    @Mock
    ValueMap valuemap;
    public List < Accordion > accordionList = new ArrayList < Accordion > ();
    String count = "6";
    //max count, based on this count loop execute and get/set into the list  
    @Before
    public void setUp() throws Exception {
        when(request.getResource()).thenReturn(resource);
        when(resource.getValueMap()).thenReturn(valuemap);
    }
    @Test
    public void shouldReturnNullWhenPropertyIsNull() throws LoginException, JSONException {
        when(valuemap.get("count", String.class)).thenReturn(null);
        accordionModel.init();
        assertEquals(accordionModel.getAccordionList(), null);
    }
    @Test
    public void shouldReturnWhenPropertyNotNull() throws LoginException, JSONException {
        when(valuemap.get("count", String.class)).thenReturn("count");
        accordionModel.init();
        assertEquals(accordionModel.getAccordionList(), count);
    }
}

Errors in program its showing line--> accordionModel.init();

    java.lang.NumberFormatException: For input string: "count"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.valueOf(Unknown Source)
    at com..services.sling.models.AccordionModel.init(AccordionModel.java:44) at   
    com..services.sling.models.AccordionModelTest.
    shouldReturnWhenPropertyNotNull(AccordionModelTest.java:55)

    java.lang.AssertionError: expected:<[]> but was:<null>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com..services.sling.models.AccordionModelTest.
    shouldReturnNullWhenPropertyIsNull(AccordionModelTest.java:53)
Lex Webb
  • 2,772
  • 2
  • 21
  • 36
gopokho
  • 1
  • 1

1 Answers1

1

java.lang.AssertionError: expected:<[]> but was:<null>

If you return null your list is empty. So adjust your test. Consider renaming the method name as well. If thats not what you want, you'll need to change your implementation.

@Test
public void shouldReturnNullWhenPropertyIsNull() throws LoginException, JSONException {
    when(valuemap.get("count", String.class)).thenReturn(null);
    accordionModel.init();
    assertTrue(accordionModel.getAccordionList().isEmpty());
}

java.lang.NumberFormatException: For input string: "count"

"count" can not be converted into an Integer. Try using your count variable ("6") instead.

You should check the content of the list, for now I adjusted it to check that the list has the correct size.

@Test
public void shouldReturnWhenPropertyNotNull() throws LoginException, JSONException {
    when(valuemap.get("count", String.class)).thenReturn(count);
    accordionModel.init();
    assertEquals(Integer.valueOf(count), accordionModel.getAccordionList().size());
}

Note that generally the parameter order for assert's should be expected vs actual.

second
  • 4,069
  • 2
  • 9
  • 24