在运行测试时出现错误,我无法理解为什么会出现此错误,此代码在 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 = "[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());
}
}
例外:
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)
在 Java 9 及更高版本中,模块系统可能会导致这些错误。我在 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
.
按如下方式启动 JVM:
# --add-opens has the following syntax: {A}/{package}={B}
java --add-opens java.base/java.lang=ALL-UNNAMED
卸载 openjdk 19 为我解决了这个错误。
$ brew uninstall maven openjdk@19
$ brew install --ignore-dependencies maven
在 IntelliJ 中,我转到“编辑配置”以打开运行/调试配置,在那里我明确指定“Java8”作为我的版本。