为什么javafx警报showAndWait在模拟中被调用?

为什么javafx警报showAndWait在模拟中被调用?,javafx,mockito,Javafx,Mockito,我想测试调用的Alert showAndWait方法。我用Mockito模拟警报并验证是否调用了showAndWait。但问题是Alert.showAndWait实际上被调用,而不是mock方法。我想这不应该发生 我的代码: package drakonli.jcomponents.notificator; import javafx.scene.control.Alert; import junit_util.JavaFXThreadingRule; import org.junit.Rul

我想测试调用的Alert showAndWait方法。我用Mockito模拟警报并验证是否调用了showAndWait。但问题是Alert.showAndWait实际上被调用,而不是mock方法。我想这不应该发生

我的代码:

package drakonli.jcomponents.notificator;

import javafx.scene.control.Alert;
import junit_util.JavaFXThreadingRule;
import org.junit.Rule;
import org.junit.Test;

import static org.mockito.Mockito.mock;

    public class AlertTest
    {
        @Rule public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();

        @Test
        public void success()
        {
            Alert alert = mock(Alert.class);

            alert.showAndWait();

            verify(alert, atLeastOnce()).showAndWait();
        }
    }
例外情况:

java.lang.NullPointerException
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.control.Dialog.showAndWait(Dialog.java:331)
    at drakonli.jcomponents.notificator.AlertNotificatorTest.success(AlertNotificatorTest.java:30)
    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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at junit_util.JavaFXThreadingRule$OnJFXThreadStatement$1.run(JavaFXThreadingRule.java:65)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:748)

Mockito无法模拟最终的方法或类。Powermock可以,但我不建议任何新功能过度使用或使用不当。我建议为框架寻找测试工具,或者绕过限制,接受一些无法有效测试的东西

方法是最终的吗?确实是。它也是一个父方法:Mockito不能覆盖最终的方法,实际上,Mockito通过创建对象的扩展名m,ockedYe来工作,在你的回答之后,我用谷歌搜索了它。太好了,谢谢。发布一个答案,我将标记为正确答案:可能重复的