首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么javafx Alert showAndWait会在模拟中被调用?

为什么javafx Alert showAndWait会在模拟中被调用?
EN

Stack Overflow用户
提问于 2018-08-26 06:27:53
回答 1查看 193关注 0票数 0

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

我的代码:

代码语言:javascript
复制
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();
        }
    }

例外:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-26 06:53:35

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52021708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档