0

I want to test that Alert showAndWait() method gets called. I mock Alert with Mockito and verify if showAndWait() was called. But the problem is Alert.showAndWait() ACTUALLY gets called, instead of mocked method. I assume this should NOT happen.

My code:

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();
        }
    }

Exception:

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)
drakonli
  • 2,304
  • 23
  • 29

1 Answers1

1

Mockito cannot mock Final methods or classes. Powermock can, but I do not recommended for any new functionality (it gets overused, or used incorrectly). I'd recommened looking for test harnesses for the framework or working around the limitations and acceptable that some stuff cannot be effectively tested.

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54