9

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 = "project.user@email.com";

@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)
The_new_coder
  • 263
  • 2
  • 3
  • 13

5 Answers5

13

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.

Rob Spoor
  • 6,186
  • 1
  • 19
  • 20
  • 1
    It solves the previous error but it gives another exception java.lang.reflect.InaccessibleObjectException: Unable to make private static long java.util.UUID.parse4Nibbles(java.lang.String,int) accessible: module java.base does not "opens java.util" to unnamed module @3d8c7aca – The_new_coder Mar 01 '22 at 14:37
  • 1
    You'll need to add an `argLine` like that one for each package that is reported. Note that this is caused because PowerMock is doing stuff it shouldn't be doing. Is there perhaps a newer version that you can use? – Rob Spoor Mar 01 '22 at 16:19
  • How can I do this with gradle and TestNG? – Ayush Shukla Jul 06 '22 at 06:58
  • I can't help you with that, as I haven't worked with either of them. – Rob Spoor Jul 06 '22 at 13:47
  • 2
    Surefire was picking up one of the `argLine` (and working) but not the other packages until I saw this: https://stackoverflow.com/a/64920692/2336934. (only 1 `` tag needed for all `--add-opens` and other arguments) – dan Jan 11 '23 at 15:07
7

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?

Mario Petrovic
  • 198
  • 1
  • 11
1

Uninstalling openjdk 19 solved this error for me.

$ brew uninstall maven openjdk@19
$ brew install --ignore-dependencies maven
user674669
  • 10,681
  • 15
  • 72
  • 105
0

In IntelliJ, I went to "Edit Configuration" for opening the Run/Debug Configurations and there I explicitly gave "Java8" as my version.

Ayushi Jain
  • 828
  • 8
  • 17
0

For Android Studio you have to provide a VM option in the configuration where you can put --add-opens <Module_Name>/<Package_of_Module>=ALL_UNNAMED.

If you can not see the VM option input box then go to the Modify option and check "Add VM Options" under the Java menu

This solved my issue

Vinayak Khedkar
  • 504
  • 5
  • 13