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

JUnit test Cases failed

Avatar

Level 2

Hii, I am using assertEquals(expected, actual ) but when I run my test case my actual value was showing as null.

Error:

Prachi_Mathur_0-1650969687355.png

 

 

Sling Model:

 

Prachi_Mathur_1-1650969766087.png

 

5 Replies

Avatar

Community Advisor

Hi @Prachi_Mathur 

I don't see the getDescription() method in your sling model. Seems to me either case of missing method or wrong naming convention in JUnit test cases.

 

Thanks,

Pallavi Shukla

Avatar

Community Advisor

Hi, @Prachi_Mathur ,

 

Could you please share your JUnit code? or otherwise try the below approach. I use the same approach for JUnit test cases. I modified my code as per your model screenshot.

 

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.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class AboutModelTest {

    private final AemContext aemContext = new AemContext();
    private About about;

    @BeforeEach
    void setUp() {
        aemContext.addModelsForClasses(About.class);
        aemContext.load().json("/com/mySite/core/models/about.json", "/component");
        Resource resource = aemContext.currentResource("/component/about");
        about=aemContext.request().adaptTo(About.class);
    }

    @Test
    void testGetEmail() {
        final String expected = "abc@gmail.com";
        assertEquals(expected, about.getEmail());
    }

    @Test
    void testGetAddress() {
        final String expected = "India";
        assertEquals(expected, about.getAddress());
    }
}

and create aboutModel.json file under core/src/test/resource folder

 

{
  "aboutModel": {
    "email":"abc@gmail.com",
    "address": "india"
  }
}

 

 

Avatar

Level 2

Prachi_Mathur_5-1650979982102.png

Still getting this error 

 

This the test code

package com.rove.core.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.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith({AemContextExtension.class, MockitoExtension.class})
class AboutTest {

    private final AemContext aemContext = new AemContext();
    private About about;

    @BeforeEach
    void setUp() {
        aemContext.addModelsForClasses(About.class);
        aemContext.load().json("/com/rove/core/core/models/about.json", "/component");
        Resource resource = aemContext.currentResource("/component/about");
        about=aemContext.request().adaptTo(About.class);
    }

    @Test
    void testGetEmail() {
        final String expected = "abc@gmail.com";
        assertEquals(expected, about.getEmail());
    }

    @Test
    void testGetAddress() {
        final String expected = "India";
        assertEquals(expected, about.getAddress());
    }
}
 
Sling Model Code
package com.rove.core.core.models;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.jcr.Node;
import javax.jcr.RepositoryException;


import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Optional;
import org.apache.sling.models.annotations.Path;
import org.apache.sling.models.annotations.injectorspecific.ChildResource;
import org.apache.sling.models.annotations.injectorspecific.ResourcePath;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.injectorspecific.ValueMapValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Model(adaptables = Resource.class)
public class About{
    private static final Logger LOGGER = LoggerFactory.getLogger(About.class);

    @Inject
    private String email;

    @Inject
    private String address;


    public String getEmail() {
        return email;
    }

    public String getAddress() {
        return address;
    }


}

Avatar

Community Advisor

Hi @Prachi_Mathur ,

Did you added  .json file in your test folder instead of core modal 

Kr,

Sanjay