Mockito und Vaadin - sehr seltsames Verhalten in Intellij

LimDul

Top Contributor
Gegen folgender Code:

Java:
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.UI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class DummyTest {

    @BeforeEach
    public void setUp() {
        UI ui = Mockito.spy(new UI());
        Mockito.doNothing().when(ui).navigate((Class<? extends Component>) Mockito.any());
    }

    @Test
    public void test() {
        System.out.println("Hier");
    }
}
Ziemlich wenig Code. Aber, wenn ich abwechselnd in intellij alle Tests oder test einzeln ausführe erhalte ich folgende Fehlermeldung:

org.mockito.exceptions.base.MockitoException:
Only void methods can doNothing()!
Example of correct use of doNothing():
doNothing().
doThrow(new RuntimeException())
.when(mock).someVoidMethod();
Above means:
someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called

at DummyTest.setUp(DummyTest.java:12)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptLifecycleMethod(TimeoutExtension.java:126)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptBeforeEachMethod(TimeoutExtension.java:76)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeMethodInExtensionContext(ClassBasedTestDescriptor.java:506)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$synthesizeBeforeEachMethodAdapter$21(ClassBasedTestDescriptor.java:491)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeEachMethods$3(TestMethodTestDescriptor.java:171)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$6(TestMethodTestDescriptor.java:199)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:199)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeEachMethods(TestMethodTestDescriptor.java:168)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:131)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
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)

Und ich sitze hier und denke Häh? Kann mir das irgendwer erklären.
 

httpdigest

Top Contributor
Die Fehlermeldung sagt doch ganz genau, was los ist.
Only void methods can doNothing()!
com.vaadin.flow.component.UI::navigate(Class) ist eben keine void-Methode, hat also einen von void unterschiedlichen Rückgabetyp (nämlich java.util.Optional).

Wenn du willst, dass der Spy auf com.vaadin.flow.component.UI::navigate(Class) etwas anderes macht als sein Delegate, dann musst du das mit doReturn() angeben, z.B.:
Java:
Mockito.doReturn(Optional.empty()).when(ui).navigate((Class<? extends Component>) Mockito.any());
 

LimDul

Top Contributor
Schenkt mir mal wer eine neue Brille ... Ja, die Methode ist nicht void. Nur die mit String Parameter ...

Warum es aber in ca. 50% der Fälle ohne Fehler läuft, ist mir nicht klar ...
 
Zuletzt bearbeitet:
Ähnliche Java Themen
  Titel Forum Antworten Datum
Rakshan mockito - negativeTest for applicationEventPublisher Frameworks - Spring, Play, Blade, Vaadin & Co 5
Oneixee5 Warum sollte man Vaadin einsetzen? Frameworks - Spring, Play, Blade, Vaadin & Co 5
OnDemand Vaadin Pro & TypScript vs Plain Java Frameworks - Spring, Play, Blade, Vaadin & Co 4
OnDemand Suche "Wizard" für Vaadin für Unterstützung bei Erstbenutzung von Funktionen Frameworks - Spring, Play, Blade, Vaadin & Co 2
Zrebna Vaadin: ToolbarButton - Breite für Caption reicht nicht aus. Frameworks - Spring, Play, Blade, Vaadin & Co 1
OnDemand Vaadin+Spring Boot erster Seitenload nach Neustart endlos Frameworks - Spring, Play, Blade, Vaadin & Co 0
Zrebna Vaadin: Lassen sich nur Panels, aber keine Windows zu TabSheets hinzufügen? Frameworks - Spring, Play, Blade, Vaadin & Co 0
Zrebna Vaadin: Falls ToolbarButton nicht enabled ist, dann nicht klickbar? Frameworks - Spring, Play, Blade, Vaadin & Co 2
Zrebna Vaadin: Kann man das ContextMenu auch via links-Klick befüllen? Frameworks - Spring, Play, Blade, Vaadin & Co 0
OnDemand Vaadin NPM Build wirft Warnungen - wie beheben? Frameworks - Spring, Play, Blade, Vaadin & Co 2
OnDemand Spring+Vaadin App startet nicht Frameworks - Spring, Play, Blade, Vaadin & Co 1
OnDemand Spring Security/Boot/Vaadin Cookie Problem bei iFrame Frameworks - Spring, Play, Blade, Vaadin & Co 4
Zrebna Vaadin 8 -> Binders nur mit Datentyp String? Frameworks - Spring, Play, Blade, Vaadin & Co 16
Zrebna Vaadin mit Jetty Server -> ERROR: Jetty server existing Frameworks - Spring, Play, Blade, Vaadin & Co 2
Zrebna Erste Steps mit Vaadin - via HotSwapAgent nach Code-Änderungen, Server-Restart vermeiden Frameworks - Spring, Play, Blade, Vaadin & Co 0
pkm Wie kann man in Vaadin einer Zeile mit ThemeResource einen ClickEventListener geben? Frameworks - Spring, Play, Blade, Vaadin & Co 0
pkm Custom-css in Vaadin greift nicht. Frameworks - Spring, Play, Blade, Vaadin & Co 1
OnDemand Vaadin globaler Event Handler Frameworks - Spring, Play, Blade, Vaadin & Co 1
OnDemand Vaadin Loading Spinner zeigt es nicht an Frameworks - Spring, Play, Blade, Vaadin & Co 1
OnDemand Spring Boot + Vaadin API Security Frameworks - Spring, Play, Blade, Vaadin & Co 1
OnDemand Vaadin Session Handling Frameworks - Spring, Play, Blade, Vaadin & Co 2
OnDemand Vaadin RouteLink Keycloak Frameworks - Spring, Play, Blade, Vaadin & Co 1
OnDemand Vaadin subscription lohnenswert? Frameworks - Spring, Play, Blade, Vaadin & Co 3

Ähnliche Java Themen

Neue Themen


Oben