我的问题是当我将 Powermock 与 ApplicationTest 或 GuiTest 一起使用时。我给 Native Library D:\jdk 1.8.0 181\jre1.8.0_181\bin\glass.dll 已经加载到另一个类加载器中。所以我必须在不使用 ApplicationTest 或 GuiTest 的情况下展示舞台。或者有没有解决方案。我尝试https://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html解决方案,但它没有解决我的问题。我无法运行我的单元测试。
public class AlertMessagesTest extends TestFXGuiBase{
@Test
public void setMessage() throws Exception {
Platform.runLater(() -> AlertMessages.setMessage(AlertMessages.AlertMessagesType.NAME, ""));
functions.sleep(1000);
functions.clickOn("Ok");
}}
public class AreaController extends SystemAdministrationManagement {
static {
System.loadLibrary ("glass.dll");
}
public void loadAreaManagement() throws Exception {
super.addSearchIconToTextField(filterText);
setVersion();
data = FXCollections.observableArrayList();
filteredData = new FilteredList(getData(), p -> true);
addAreaNadModeListToTableView();
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({Database.class, AlertManager.class, AreaController.class})
@SuppressStaticInitializationFor("gov.sysadmin.areaManagement.AreaController")
public class AreaControllerTest extends TestFXGuiBase {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/areaManagement/area.fxml"));
@Test
public void showAreaNadModeListTestFunction() throws Exception{
TrainLine trainLine = new TrainLine();
trainLine.setId(1);
trainLine.setName("Name");
trainLine.setExp("Explanation");
Area firstArea = new Area();
firstArea.setId(1);
firstArea.setShortname("BSM");
firstArea.setLongname("BSM");
firstArea.setTrainline(1);
firstArea.setOrderno((byte) 1);
firstArea.setAddressxml(null);
firstArea.setAreaxml(null);
firstArea.setExplanation("BSM");
firstArea.setTrainlineByTrainline(trainLine);
firstArea.setIsautonad(false);
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Date secondAreaDayStartTime = dateFormat.parse("08:00:00");
Date secondAreaNightStartTime = dateFormat.parse("21:00:00");
Area secondArea = new Area();
secondArea.setId(2);
secondArea.setShortname("BND");
secondArea.setLongname("BND");
secondArea.setTrainline(1);
secondArea.setOrderno((byte) 2);
secondArea.setAddressxml(null);
secondArea.setAreaxml(null);
secondArea.setExplanation("");
secondArea.setTrainlineByTrainline(trainLine);
secondArea.setDaystart(new Time(secondAreaDayStartTime.getTime()));
secondArea.setDaystart(new Time(secondAreaNightStartTime.getTime()));
secondArea.setIsautonad(true);
List<Area> areaList = new ArrayList<Area>();
areaList.add(firstArea);
areaList.add(secondArea);
PowerMock.suppressConstructor(Database.class);
PowerMock.mockStatic(Database.class);
Database mockDatabase = EasyMock.createMock(Database.class);
EasyMock.expect(Database.getInstance()).andReturn(mockDatabase).anyTimes();
EasyMock.expect(mockDatabase.getAllArea()).andReturn(areaList);
EasyMock.expectLastCall().times(1);
PowerMock.replay(Database.class);
PowerMock.replay(mockDatabase);
AreaController areaNadModeListController = fxmlLoader.getController();
AreaController spy = Mockito.spy(areaNadModeListController);
Mockito.doNothing().when(spy).setVersion();
spy.loadAreaManagement();
EasyMock.verify(mockDatabase);
}
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.UnsatisfiedLinkError: Native Library D:\jdk 1.8.0 181\jdk1.8.0_181\jre\bin\glass.dll already loaded in another classloader
at org.testfx.util.WaitForAsyncUtils.waitFor(WaitForAsyncUtils.java:284)
at org.testfx.api.FxToolkit.waitForLaunch(FxToolkit.java:323)
at org.testfx.api.FxToolkit.registerPrimaryStage(FxToolkit.java:132)
at org.testfx.framework.junit.ApplicationTest.internalBefore(ApplicationTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:129)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:93)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
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.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.RuntimeException: java.lang.UnsatisfiedLinkError: Native Library D:\jdk 1.8.0 181\jdk1.8.0_181\jre\bin\glass.dll already loaded in another classloader
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:267)
at com.sun.javafx.application.PlatformImpl.startup(PlatformImpl.java:211)
at com.sun.javafx.application.LauncherImpl.startToolkit(LauncherImpl.java:675)
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:695)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.UnsatisfiedLinkError: Native Library D:\jdk 1.8.0 181\jdk1.8.0_181\jre\bin\glass.dll already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1907)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
at java.lang.Runtime.load0(Runtime.java:809)
at java.lang.System.load(System.java:1086)
at com.sun.glass.utils.NativeLibLoader.loadLibraryFullPath(NativeLibLoader.java:201)
at com.sun.glass.utils.NativeLibLoader.loadLibraryInternal(NativeLibLoader.java:94)
at com.sun.glass.utils.NativeLibLoader.loadLibrary(NativeLibLoader.java:39)
at com.sun.glass.ui.Application.loadNativeLibrary(Application.java:112)
at com.sun.glass.ui.Application.loadNativeLibrary(Application.java:120)
at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:40)
at com.sun.glass.ui.win.WinApplication$1.run(WinApplication.java:104)
at com.sun.glass.ui.win.WinApplication$1.run(WinApplication.java:90)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.glass.ui.win.WinApplication.<clinit>(WinApplication.java:90)
at com.sun.glass.ui.win.WinPlatformFactory.createApplication(WinPlatformFactory.java:39)
at com.sun.glass.ui.win.WinPlatformFactory.createApplication(WinPlatformFactory.java:36)
at com.sun.glass.ui.Application.run(Application.java:146)
at com.sun.javafx.tk.quantum.QuantumToolkit.startup(QuantumToolkit.java:257)
... 5 more
第一个是我的课。第二个是我的 AreaController 类的测试类,最后一个是异常。