我尝试使用 @ExtendWith(ApplicationExtension.class) 运行测试,但我不断收到此错误
java.lang.reflect.InaccessibleObjectException: Unable to make field private static java.util.Map com.sun.javafx.application.ParametersImpl.params accessible: module javafx.graphics does not "opens com.sun.javafx.application" to unnamed module @2133814f
at java.base/java.lang.reflect.AccessibleObject.throwInaccessibleObjectException(AccessibleObject.java:387)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:363)
at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:311)
at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:181)
at java.base/java.lang.reflect.Field.setAccessible(Field.java:175)
at org.testfx.toolkit.impl.ToolkitServiceImpl.cleanupParameters(ToolkitServiceImpl.java:177)
at org.testfx.toolkit.impl.ToolkitServiceImpl.cleanupApplication(ToolkitServiceImpl.java:138)
at org.testfx.api.FxToolkit.cleanupApplication(FxToolkit.java:178)
at org.testfx.api.FxToolkit.cleanupAfterTest(FxToolkit.java:186)
at org.testfx.framework.junit5.ApplicationExtension.afterEach(ApplicationExtension.java:87)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachCallbacks$12(TestMethodTestDescriptor.java:261)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:277)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$14(TestMethodTestDescriptor.java:277)
at org.junit.platform.commons.util.CollectionUtils.forEachInReverseOrder(CollectionUtils.java:217)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:276)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:260)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:145)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:198)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:169)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:93)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:58)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:141)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:57)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:103)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:85)
at org.junit.platform.launcher.core.DelegatingLauncher.execute(DelegatingLauncher.java:47)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:63)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
这是我的代码
package gui;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testfx.framework.junit5.ApplicationExtension;
@ExtendWith(ApplicationExtension.class)
public class AddTripTest {
@Test
void demo(){
}
}
我使用 intelij java 20 和 maven
我只是想摆脱这个错误。如果我摆脱 @ExtendWith(ApplicationExtension.class) 它确实有效,但我需要 @ExtendWith(ApplicationExtension.class) 来实际编写测试
看起来您丢失了
module-info.java
您需要将其放入主包文件夹并定义应导出的内容
module swaggerific {
requires javafx.controls;
requires javafx.graphics;
requires javafx.fxml;
requires javafx.web;
requires javafx.base;
requires javafx.media;
requires javafx.swing;
//////....
opens com.mascix.swaggerific to javafx.fxml;
exports com.mascix.swaggerific;
exports com.mascix.swaggerific.animation;
opens com.mascix.swaggerific.animation to javafx.fxml;
exports com.mascix.swaggerific.ui;
opens com.mascix.swaggerific.ui to javafx.fxml;
exports com.mascix.swaggerific.data;
opens com.mascix.swaggerific.data to javafx.fxml;
exports com.mascix.swaggerific.ui.component;
opens com.mascix.swaggerific.ui.component to javafx.fxml;
exports com.mascix.swaggerific.ui.edit;
opens com.mascix.swaggerific.ui.edit to javafx.fxml;
exports com.mascix.swaggerific.ui.textfx;
opens com.mascix.swaggerific.ui.textfx to javafx.fxml;
exports com.mascix.swaggerific.ui.exception;
opens com.mascix.swaggerific.ui.exception to javafx.fxml;
}
您可以在此处找到完整的工作示例。您可以在此处找到工作示例测试代码。我也会给出下面的示例代码
package com.mascix.swaggerific;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testfx.api.FxAssert;
import org.testfx.api.FxRobot;
import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.Start;
import org.testfx.matcher.control.LabeledMatchers;
@ExtendWith(ApplicationExtension.class)
class ClickableButtonTest_JUnit5Hamcrest {
private Button button;
/**
* Will be called with {@code @Before} semantics, i. e. before each test method.
*
* @param stage - Will be injected by the test runner.
*/
@Start
private void start(Stage stage) {
button = new Button("click me!");
button.setId("myButton");
button.setOnAction(actionEvent -> button.setText("clicked!"));
stage.setScene(new Scene(new StackPane(button), 100, 100));
stage.show();
}
/**
* @param robot - Will be injected by the test runner.
*/
@Test
void should_contain_button_with_text(FxRobot robot) {
FxAssert.verifyThat(button, LabeledMatchers.hasText("click me!"));
// or (lookup by css id):
FxAssert.verifyThat("#myButton", LabeledMatchers.hasText("click me!"));
// or (lookup by css class):
FxAssert.verifyThat(".button", LabeledMatchers.hasText("click me!"));
}
/**
* @param robot - Will be injected by the test runner.
*/
@Test
void when_button_is_clicked_text_changes(FxRobot robot) {
// when:
robot.clickOn(".button");
// then:
FxAssert.verifyThat(button, LabeledMatchers.hasText("clicked!"));
// or (lookup by css id):
FxAssert.verifyThat("#myButton", LabeledMatchers.hasText("clicked!"));
// or (lookup by css class):
FxAssert.verifyThat(".button", LabeledMatchers.hasText("clicked!"));
}
}