AEM 6.5 Java 11 upgrade: Junit tests with MockSlingHttpServletRequest adaptTo() failing | Community
Skip to main content
March 19, 2020
Solved

AEM 6.5 Java 11 upgrade: Junit tests with MockSlingHttpServletRequest adaptTo() failing

  • March 19, 2020
  • 1 reply
  • 844 views

On upgrading from Java 8 to Java 11, all unit tests with request.adaptTo() have started failing with NPE - code snippet below. We are using Junit 4 and mockito-core version 2.25.1. Please advice on this issue. Do we need to add additional dependencies when upgrading to Java 11? TIA.

 

import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest;
Resource resource = resolver.getResource("/abc-component/main_abc_component");
request.setResource(resource);
Abc abc = request.adaptTo(Abc.class);

 

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 aanchal-sikka

Please refer to the unit test below

import org.apache.sling.api.resource.Resource; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.apache.sling.testing.mock.sling.junit.SlingContext; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.mockito.Mock; import static org.junit.Assert.*; import static org.mockito.Mockito.*; public class YourComponentTest { @Rule public SlingContext context = new SlingContext(ResourceResolverType.JCR_MOCK); @Mock private Resource resource; @Before public void setUp() { context.load().json("/your-test-resource.json", "/abc-component"); } @2785667 public void testAdaptToAbc() { MockSlingHttpServletRequest request = context.request(); request.setResource(resource); // Mock Abc class Abc abc = mock(Abc.class); // Assume that your adaptTo method in Abc class returns abc instance when(request.adaptTo(Abc.class)).thenReturn(abc); // Perform the actual adaptation Abc adaptedAbc = request.adaptTo(Abc.class); // Verify that the adaptation was successful assertNotNull(adaptedAbc); // You can also verify that certain methods or behaviors on the mock Abc object are called as expected verify(abc).someMethod(); // Add your test assertions here to check the behavior of the adapted Abc object. } }

1 reply

aanchal-sikka
Community Advisor
aanchal-sikkaCommunity AdvisorAccepted solution
Community Advisor
October 30, 2023

Please refer to the unit test below

import org.apache.sling.api.resource.Resource; import org.apache.sling.testing.mock.sling.ResourceResolverType; import org.apache.sling.testing.mock.sling.servlet.MockSlingHttpServletRequest; import org.apache.sling.testing.mock.sling.junit.SlingContext; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.mockito.Mock; import static org.junit.Assert.*; import static org.mockito.Mockito.*; public class YourComponentTest { @Rule public SlingContext context = new SlingContext(ResourceResolverType.JCR_MOCK); @Mock private Resource resource; @Before public void setUp() { context.load().json("/your-test-resource.json", "/abc-component"); } @2785667 public void testAdaptToAbc() { MockSlingHttpServletRequest request = context.request(); request.setResource(resource); // Mock Abc class Abc abc = mock(Abc.class); // Assume that your adaptTo method in Abc class returns abc instance when(request.adaptTo(Abc.class)).thenReturn(abc); // Perform the actual adaptation Abc adaptedAbc = request.adaptTo(Abc.class); // Verify that the adaptation was successful assertNotNull(adaptedAbc); // You can also verify that certain methods or behaviors on the mock Abc object are called as expected verify(abc).someMethod(); // Add your test assertions here to check the behavior of the adapted Abc object. } }
Aanchal Sikka