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")); } }
Solved! Go to Solution.
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
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
Views
Likes
Replies