Junit5 shows 0% coverage in Intellij help on cover the line | Community
Skip to main content
Level 2
November 15, 2022
Solved

Junit5 shows 0% coverage in Intellij help on cover the line

  • November 15, 2022
  • 1 reply
  • 590 views

Hi All,

We have created Junit5 class for the below code.

We covered all line except 40, 47 and 48, can anyone help to cover line no 40, 47 and 48?

 

 

 

Test Class:-

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"));
    }
}
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 Shubham_borole

For catching exception can you try something like below? Not a direct solution to your problem just an example

 

@Test
	void testinitException() throws Exception {
		when(myModel.method()).thenThrow(NullPointerException.class);
		
		assertNotNull(mymodel);
	}

Basically in the test method we throw an exception explicitly and make sure its caught and handled

1 reply

Shubham_borole
Community Advisor
Shubham_boroleCommunity AdvisorAccepted solution
Community Advisor
November 15, 2022

For catching exception can you try something like below? Not a direct solution to your problem just an example

 

@Test
	void testinitException() throws Exception {
		when(myModel.method()).thenThrow(NullPointerException.class);
		
		assertNotNull(mymodel);
	}

Basically in the test method we throw an exception explicitly and make sure its caught and handled