0

I'm trying to revive an older java repo that needs some changes to be made. I am no java pro by far, but alas it must be done.

The repo specifies OpenJDK-14.0.1 and Gradle-6.5 but I have decided to get it running on a newer version of Java and Gradle so that the next poor soul that comes along may have an easier chance of getting setup.

As such I've gone with OpenJDK-19.0.1 and Gradle-7.5.1 and expectedly there have been some issues.

One being the compile configuration having been removed from Gradle-7.5.1 replaced with implementation and the second needing to update the version for Lombok to a higher version so I have gone with 1.18.24

Now the repo builds and 321 of 330 tests pass with 9 failing. This is where I am stuck. All the failing tests are all in the same file and fail because of the same error. Not being a java pro this error doesn't mean much to me.

So my questions is what causes this error in Java and how is it fixed?

The error and a stripped back version of the code with only one of the failing tests is supplied below.

ERROR

java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.Map java.util.Collections$UnmodifiableMap.m accessible: module java.base does not "opens java.util" to unnamed module @48cf768c
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
    at org.junit.contrib.java.lang.system.EnvironmentVariables.getFieldValue(EnvironmentVariables.java:136)
    at org.junit.contrib.java.lang.system.EnvironmentVariables.getEditableMapOfVariables(EnvironmentVariables.java:98)
    at org.junit.contrib.java.lang.system.EnvironmentVariables.access$000(EnvironmentVariables.java:34)
    at org.junit.contrib.java.lang.system.EnvironmentVariables$EnvironmentVariablesStatement.restoreOriginalVariables(EnvironmentVariables.java:82)
    at org.junit.contrib.java.lang.system.EnvironmentVariables$EnvironmentVariablesStatement.evaluate(EnvironmentVariables.java:73)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
    at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at jdk.internal.reflect.GeneratedMethodAccessor124.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
    at jdk.proxy2/jdk.proxy2.$Proxy5.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker$2.run(TestWorker.java:176)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.executeAndMaintainThreadName(TestWorker.java:129)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:100)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.execute(TestWorker.java:60)
    at org.gradle.process.internal.worker.child.ActionExecutionWorker.execute(ActionExecutionWorker.java:56)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:133)
    at org.gradle.process.internal.worker.child.SystemApplicationClassLoaderWorker.call(SystemApplicationClassLoaderWorker.java:71)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.run(GradleWorkerMain.java:69)
    at worker.org.gradle.process.internal.worker.GradleWorkerMain.main(GradleWorkerMain.java:74)

CODE

package com.pastoralcare.filter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.EnvironmentVariables;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.authenticator.AuthenticationTokenAuthenticator;
import com.authenticator.InterServiceTokenAuthenticator;
import com.authorization.CheckStaffPermissions;
import com.dto.PermissionSetCodeDto;
import com.model.AuthToken;
import com.pastoralcare.constant.RestAPIConstants;
import com.pastoralcare.dto.AuthTokenDto;
import com.pastoralcare.dto.PermissionSetCodesDto;
import com.pastoralcare.dto.TokenDto;

@RunWith(SpringRunner.class)
public class TokenFilterTest {

    @InjectMocks
    TokenFilter tokenFilter;

    @Mock
    TokenDto tokenDto;

    @MockBean
    FilterChain filterChain;

    @Mock
    PermissionSetCodesDto permissionSetCodesDto;

    @Mock
    AuthTokenDto authTokenDto;

    @Mock
    InterServiceTokenAuthenticator internalAuth;

    @Mock
    AuthenticationTokenAuthenticator authnValidator; 

    @Mock
    CheckStaffPermissions checkStaffPermissions; 

    @Mock
    AuthToken authToken; 

    @Mock
    PermissionSetCodeDto permissionSetCodeDto;

    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();

    @Rule
    public final EnvironmentVariables environmentVariables = new EnvironmentVariables();

    PermissionSetCodesDto permissionSetsDto;
    
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        //      tokenFilter = new TokenFilter();
        tokenFilter.tokenDto = mock(TokenDto.class);

        UUID communityId = UUID.randomUUID();
        UUID tenantId = UUID.randomUUID();
        tokenFilter.tokenDto.setTenantId(tenantId);
        tokenFilter.tokenDto.setCommunityId(communityId);
        tokenFilter.tokenDto.setFirstName("name");
        tokenFilter.tokenDto.setLastName("surname");
        tokenFilter.tokenDto.setTimezoneIanaCode("UTC");
        
        ReflectionTestUtils.setField(tokenFilter, "interServiceAuthKey", "8Y419dV49Jw+yeRho5iUIgLErSztNnlWvfmc2vzi8D8=");
        ReflectionTestUtils.setField(tokenFilter, "activeProfile", "activeProfile");

        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(httpServletRequest));
    }

    @Test
    public void testDoFilterActuator() throws ServletException, IOException {
        httpServletRequest.setRequestURI("/actuator/");
        MockHttpServletResponse mockResp = new MockHttpServletResponse();
        FilterChain mockFilterChain = Mockito.mock(FilterChain.class);
        tokenFilter.doFilter(httpServletRequest, mockResp, mockFilterChain);
        assertEquals(200, mockResp.getStatus());
    }

}
jimorey
  • 1
  • 1
  • 1
    Withhout really wanting to dive into it, there have been changes to Java over the years that allow less and less dirty hacks. You may be better off just leaving it on that version and letting someone spend time on it that knows more about Java. Or you may consider going to Java 17 which is an LTS version with a far higher adoption than 19 which will soon lose support anyway. If it's a repo you rarely touch, I'd just go with 17 now and the next LTS (21) when it comes out and there is a need. – Sebastiaan van den Broek Oct 21 '22 at 09:42
  • @SebastiaanvandenBroek good to know. I did try Java 17 and got the the exact same result. I will switch back to that version though given what you've mentioned about LTS. – jimorey Oct 21 '22 at 09:44
  • https://stackoverflow.com/questions/70756414/java-lang-reflect-inaccessibleobjectexception-unable-to-make-field-private-fina had a similar error on a version incompatibility between the JRE and Cucumber. In your case it could be an incompatibility between your JRE and some other dependency, like JUnit. – Thomas Oct 21 '22 at 10:05

1 Answers1

0

By massive coincidence I actually ran into the issue myself a few days after commenting, and thus had to solve it.

I had to replace the old Mockito library with the newer 'inline' version. This can be found at https://mvnrepository.com/artifact/org.mockito/mockito-inline

This version no longer attempts to do things that Java 17 doesn't allow anymore while trying to create a Mock object.

Sebastiaan van den Broek
  • 5,818
  • 7
  • 40
  • 73