Skip to content

Commit

Permalink
Fixes mockito#2497: Don't ignore exception if invalid matchers are pa…
Browse files Browse the repository at this point in the history
…ssed
  • Loading branch information
temp-droid committed Dec 12, 2021
1 parent 3a2eb5a commit aa8028a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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();

@Test
public void testStaticMockSimple() {
assertEquals("foo", 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;
}
}
}

0 comments on commit aa8028a

Please sign in to comment.