Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

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

Avatar

Level 1

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);

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

    @test
    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

View solution in original post

1 Reply

Avatar

Correct answer by
Community Advisor

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

    @test
    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