Home > Software design >  JUINT test giving error java.lang.reflect.InaccessibleObjectException: Unable to make protected void
JUINT test giving error java.lang.reflect.InaccessibleObjectException: Unable to make protected void

Time:03-01

While running the test I am getting the error , I am not able to understand why am I getting this error , this code is working with fine in java 8 , while running it in java 17 it is giving error. googled this error but found nothing useful. Please help me to understand this error. Thanks in advance:)

@RunWith(PowerMockRunner.class)
@PrepareForTest({PopulatedAuthorizedUser.class})
@SpringBootTest(classes = MockServletContext.class)
@PowerMockIgnore({"javax.management.*", "javax.net.ssl.*", 
"jdk.internal.reflect.*"})
public class ProjectUserControllerTest {

private ProjectUserController controller;

private UUID projectId = UUID.randomUUID();
private UUID groupId = UUID.randomUUID();
private String email = "[email protected]";

@Mock
private ProjectUserService projectUserService;

private ObjectMapper objectMapper = new ObjectMapper();

@Mock
protected AuthorizedUser au;

@Before
public void setUp() throws Exception {
    controller = new ProjectUserController();
    FieldUtils.writeField(controller, "projectUserService", projectUserService, true);
    FieldUtils.writeField(controller, "objectMapper", objectMapper, true);
    PowerMockito.mockStatic(PopulatedAuthorizedUser.class);
    Mockito.when(PopulatedAuthorizedUser.get()).thenReturn(mockAuthorizedUser());
}

@Test
public void testGetProjectUsers() {
    Mockito.doReturn(Arrays.asList(mockProjectUser())).when(projectUserService)
            .findProjectUsersByProjectId(projectId);
    Mockito.doNothing().when(projectUserService).enrichUserDetails(any(ProjectUserDto.class));
    ResponseEntity<List<ProjectUserDto>> response=controller.getProjectUsers(projectId);
    assertNotNull(response);
    ProjectUserDto projectUserDto = response.getBody().get(0);
    assertEquals(groupId, projectUserDto.getGroupId());
    assertEquals(email, projectUserDto.getUsername());
    assertTrue(projectUserDto.getEmailNotification());
    assertEquals(ProjectUserRole.OWNER.toString(), projectUserDto.getRole());
   }

 }

Exception:

 java.lang.reflect.InaccessibleObjectException: Unable to make protected void java.lang.Object.finalize() throws java.lang.Throwable accessible: module java.base does not "opens java.lang" to unnamed module @5ba23b66

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.Method.checkCanSetAccessible(Method.java:199)
at java.base/java.lang.reflect.Method.setAccessible(Method.java:193)
at org.powermock.reflect.internal.WhiteboxImpl.doGetAllMethods(WhiteboxImpl.java:1492)
at org.powermock.reflect.internal.WhiteboxImpl.getAllMethods(WhiteboxImpl.java:1467)
at org.powermock.reflect.internal.WhiteboxImpl.findMethodOrThrowException(WhiteboxImpl.java:847)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:807)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:790)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:466)
at org.powermock.modules.junit4.common.internal.impl.PowerMockJUnit4RunListener.testFinished(PowerMockJUnit4RunListener.java:55)
at org.junit.runner.notification.SynchronizedRunListener.testFinished(SynchronizedRunListener.java:87)
at org.junit.runner.notification.RunNotifier$9.notifyListener(RunNotifier.java:225)
at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72)
at org.junit.runner.notification.RunNotifier.fireTestFinished(RunNotifier.java:222)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.testAborted(PowerMockJUnit44RunnerDelegateImpl.java:229)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:206)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

CodePudding user response:

In Java 9 and above, the module system can cause these errors. I've had the same issues with JUnit 5 itself because I omit the public from my test classes and methods.

Here's what I have in my POM to solve this:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <!-- Allow JUnit to access the test classes -->
    <argLine>--add-opens <my-module>/<my-package>=ALL-UNNAMED</argLine>
  </configuration>
</plugin>

In your case you probably need to use java.base/java.lang=ALL_UNNAMED.

CodePudding user response:

launch the JVM as follows:

# --add-opens has the following syntax: {A}/{package}={B}
java --add-opens java.base/java.lang=ALL-UNNAMED

How to solve InaccessibleObjectException ("Unable to make {member} accessible: module {A} does not 'opens {package}' to {B}") on Java 9?

  • Related