Expand my Community achievements bar.

SOLVED

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

Avatar

Level 2

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?

 

 

screenshot1.jpeg

 

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"));
    }
}
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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