junit JUINT测试出现错误java.lang.reflect.无法访问对象异常:无法使受保护的java.lang.Object.finalize()无效

o4hqfura  于 2022-11-29  发布在  Java
关注(0)|答案(3)|浏览(764)

在运行测试时,我得到了错误,我不能理解为什么我得到这个错误,这个代码在java 8中工作正常,而在java 17中运行它时,它给出错误。谷歌搜索这个错误,但没有发现任何有用的。请帮助我理解这个错误。提前感谢:)

@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());
   }

 }

例外情况:

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

y1aodyip1#

在Java9和更高版本中,模块系统可能会导致这些错误。我在JUnit 5本身也遇到过同样的问题,因为我在测试类和方法中省略了public
下面是我在POM中解决这个问题的方法:

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

在您的情况下,可能需要使用java.base/java.lang=ALL_UNNAMED

dzjeubhm

dzjeubhm2#

按如下方式启动JVM:

# --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?

rekjcdws

rekjcdws3#

卸载openjdk 19为我解决了这个错误。

$ brew uninstall maven openjdk@19
$ brew install --ignore-dependencies maven

相关问题