Junit5 shows always 0% coverage in Intellij | Community
Skip to main content
Level 2
October 28, 2022
Solved

Junit5 shows always 0% coverage in Intellij

  • October 28, 2022
  • 2 replies
  • 7655 views

Hi All,

I have created Junit5 class for the below code.

package com.test.core.models; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.Required; import org.apache.sling.models.annotations.injectorspecific.Self; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import org.apache.sling.settings.SlingSettingsService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; @Model(adaptables = SlingHttpServletRequest.class) public class RedirectionModel { @1961677 private SlingHttpServletRequest request; @SlingObject private SlingHttpServletResponse response; @586265 @Required private String redirectURL; @586265 private SlingSettingsService slingSettingsService; private static final Logger log = LoggerFactory.getLogger(RedirectionModel.class); @PostConstruct protected void init() { log.info("PageRedirectModel: init method"); Resource resource ; resource = request.getResource(); String redirectTarget = resource.getResourceResolver().map(request, redirectURL); log.info("redirectTarget Url {}",redirectTarget); if (redirectTarget != null && redirectTarget.startsWith("/content/")) { redirectTarget += ".html"; } try { log.info("in try"); response.sendRedirect(redirectTarget); } catch (IOException e) { log.error("[RedirectUtils] Failed to execute redirect", e); } } }

 

Junit5 class

package com.test.core.models; import com.day.cq.wcm.api.Page; import io.wcm.testing.mock.aem.junit5.AemContext; import io.wcm.testing.mock.aem.junit5.AemContextExtension; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletResponse; 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.Mockito; import static org.junit.jupiter.api.Assertions.*; @ExtendWith(AemContextExtension.class) public class RedirectionModelTest { public RedirectionModel redirectionModel; private Page page; private Resource resource; private String RedirectUrl; @Mock MockSlingHttpServletRequest request; @Mock MockSlingHttpServletResponse response; @BeforeEach public void setUp(AemContext context) throws Exception{ RedirectUrl="/content/mykp/homepage"; page = context.create().page("/content/mykp/en"); resource = context.resourceResolver().getResource("/content/mykp/en"); redirectionModel=resource.adaptTo(RedirectionModel.class); } @2785667 public void init() { String redirectTarget = resource.getResourceResolver().map(request, this.RedirectUrl); if(redirectTarget.startsWith("/content")&& redirectTarget!=null) { redirectTarget += ".html"; } assertEquals("/content/mykp/homepage.html",redirectTarget); } }

after run Test with code converge it is showing me always 0% in  IntelliJ.

did i miss anything?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Lokesh_Vajrala

We covered the 76.5% coverage but for our project we need 80% coverage.

Line number 40, 47 and 48 not be covered.

what we have to do for that?

 


@bhavi50 , You've to write test cases for negative/exception cases to get coverage for the uncovered lines. If you notice, the lines that are not covered in code coverage are the cases where you encounter the exception or when the path doesn't start with "/content". 

Add a new test case by creating the page under a non-content path, for instance under /conf or /test, at that time if condition at line #40 is not satisfied and will be covered. 

 

 

2 replies

Lokesh_Vajrala
Community Advisor
Community Advisor
October 29, 2022

@bhavi50 Yes, you're missing a few things. 

  1. RedirectionModel is registered with SlingHttpServletRequest, but in the RedirectionModelTest class you're trying to adapt it from Resource, which will just return null model and init() method in Sling Model is never called. 
  2. redirectURL param is not set in the request to set the Required field for redirection.
  3. init() method in test class is not really testing the RedirectionModel, instead it is testing some made up data within the method. 

 

You can refer this code to properly unit test the model and which will give you the code coverage

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 static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(AemContextExtension.class)
public class RedirectionModelTest {

    AemContext context = new AemContext();
    @BeforeEach
    public void setUp() throws Exception{

        context.create().page("/content/mykp/en");
        Resource resource = context.resourceResolver().getResource("/content/mykp/en");
        context.request().setAttribute("redirectURL", "/content/mykp/homepage");
        context.currentResource(resource);
        context.request().adaptTo(RedirectionModel.class);

    }

    @Test
    public void init() {
        assertEquals("/content/mykp/homepage.html",context.response().getHeader("location"));
    }
}
bhavi50Author
Level 2
October 29, 2022

Still it's showing 0% coverage.

 

It gives the error and JUnitt Test failed.

 

 

Lokesh_Vajrala
Community Advisor
Community Advisor
November 9, 2022

Hi @lokesh_vajrala ,

We tried multiple ways still not covered that 2 line, can you help me to cover any one line? Can you please help me?


Where was it failing? 

 

You need something like the following to cover the line #40, which should give you more than 80% coverage except the exception block. 

    // BeforeEach method runs before running any test case, so you need to create page under non-content directory to test the negative scenario.   
@Test public void testInvalidRedirectTarget() { context.create().page("/conf/mykp/en"); Resource resource = context.resourceResolver().getResource("/conf/mykp/en"); context.request().setAttribute("redirectURL", "/conf/mykp/homepage"); context.currentResource(resource); context.request().adaptTo(RedirectionModel.class); assertEquals("/conf/mykp/homepage",context.response().getHeader("location")); }

 

Siva_Sogalapalli
Community Advisor
Community Advisor
October 31, 2022

@bhavi50 

can you check if you have enabled coverage for your project in intellij setting or not or if the class has been excluded from the coverage by mistake?

https://www.jetbrains.com/help/idea/configuring-code-coverage-measurement.html