Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2497: Throw exception on invalid matchers for mockStatic #2506

Merged
merged 3 commits into from Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/org/mockito/internal/MockedStaticImpl.java
Expand Up @@ -47,6 +47,8 @@ public <S> OngoingStubbing<S> when(Verification verification) {

try {
verification.apply();
} catch (MockitoException exception) {
throw exception;
} catch (Throwable ignored) {
}

Expand Down
Expand Up @@ -7,11 +7,14 @@
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;

import java.util.concurrent.atomic.AtomicReference;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.exceptions.base.MockitoException;
Expand All @@ -20,6 +23,9 @@

public final class StaticMockTest {

@Rule
public ExpectedException exception = ExpectedException.none();
TimvdLippe marked this conversation as resolved.
Show resolved Hide resolved

@Test
public void testStaticMockSimple() {
assertEquals("foo", Dummy.foo());
Expand All @@ -46,15 +52,15 @@ public void testStaticMockWithVerificationFailed() {
}

@Test
public void testStaticMockWithMoInteractions() {
public void testStaticMockWithNoInteractions() {
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
dummy.when(Dummy::foo).thenReturn("bar");
dummy.verifyNoInteractions();
}
}

@Test(expected = NoInteractionsWanted.class)
public void testStaticMockWithMoInteractionsFailed() {
public void testStaticMockWithNoInteractionsFailed() {
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
dummy.when(Dummy::foo).thenReturn("bar");
assertEquals("bar", Dummy.foo());
Expand All @@ -63,7 +69,7 @@ public void testStaticMockWithMoInteractionsFailed() {
}

@Test
public void testStaticMockWithMoMoreInteractions() {
public void testStaticMockWithNoMoreInteractions() {
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
dummy.when(Dummy::foo).thenReturn("bar");
assertEquals("bar", Dummy.foo());
Expand All @@ -73,7 +79,7 @@ public void testStaticMockWithMoMoreInteractions() {
}

@Test(expected = NoInteractionsWanted.class)
public void testStaticMockWithMoMoreInteractionsFailed() {
public void testStaticMockWithNoMoreInteractionsFailed() {
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
dummy.when(Dummy::foo).thenReturn("bar");
assertEquals("bar", Dummy.foo());
Expand Down Expand Up @@ -177,6 +183,20 @@ public void testStaticMockVoid() {
assertEquals("bar", Dummy.var1);
}

@Test
public void testStaticMockMustUseValidMatchers() {
exception.expect(MockitoException.class);
exception.expectMessage("Invalid use of argument matchers!\n" +
"2 matchers expected, 1 recorded:\n" +
"-> at org.mockitoinline.StaticMockTest");

try (MockedStatic<Dummy> mockedClass = Mockito.mockStatic(Dummy.class)) {
mockedClass.when(() -> Dummy.fooVoid("foo", any())).thenReturn(null);

Dummy.fooVoid("foo", "bar");
}
}

static class Dummy {

static String var1 = null;
Expand All @@ -188,5 +208,9 @@ static String foo() {
static void fooVoid(String var2) {
var1 = var2;
}

static void fooVoid(String var2, String var3) {
var1 = var2;
}
}
}