Skip to content

Commit 44ba00f

Browse files
authored
Fixes #2497: Throw exception on invalid matchers for mockStatic (#2506)
1 parent 582d890 commit 44ba00f

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

src/main/java/org/mockito/internal/MockedStaticImpl.java

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public <S> OngoingStubbing<S> when(Verification verification) {
4747

4848
try {
4949
verification.apply();
50+
} catch (MockitoException exception) {
51+
throw exception;
5052
} catch (Throwable ignored) {
5153
}
5254

subprojects/inline/inline.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ apply from: "$rootDir/gradle/java-library.gradle"
55
dependencies {
66
api project.rootProject
77
testImplementation libraries.junit4
8+
testImplementation libraries.assertj
89
}
910

1011
tasks.javadoc.enabled = false

subprojects/inline/src/test/java/org/mockitoinline/StaticMockTest.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
import static junit.framework.TestCase.assertEquals;
88
import static junit.framework.TestCase.assertNull;
99
import static junit.framework.TestCase.fail;
10+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
11+
import static org.mockito.ArgumentMatchers.any;
1012
import static org.mockito.Mockito.times;
1113

1214
import java.util.concurrent.atomic.AtomicReference;
1315

16+
import org.assertj.core.api.ThrowableAssert;
1417
import org.junit.Test;
1518
import org.mockito.MockedStatic;
1619
import org.mockito.Mockito;
@@ -46,15 +49,15 @@ public void testStaticMockWithVerificationFailed() {
4649
}
4750

4851
@Test
49-
public void testStaticMockWithMoInteractions() {
52+
public void testStaticMockWithNoInteractions() {
5053
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
5154
dummy.when(Dummy::foo).thenReturn("bar");
5255
dummy.verifyNoInteractions();
5356
}
5457
}
5558

5659
@Test(expected = NoInteractionsWanted.class)
57-
public void testStaticMockWithMoInteractionsFailed() {
60+
public void testStaticMockWithNoInteractionsFailed() {
5861
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
5962
dummy.when(Dummy::foo).thenReturn("bar");
6063
assertEquals("bar", Dummy.foo());
@@ -63,7 +66,7 @@ public void testStaticMockWithMoInteractionsFailed() {
6366
}
6467

6568
@Test
66-
public void testStaticMockWithMoMoreInteractions() {
69+
public void testStaticMockWithNoMoreInteractions() {
6770
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
6871
dummy.when(Dummy::foo).thenReturn("bar");
6972
assertEquals("bar", Dummy.foo());
@@ -73,7 +76,7 @@ public void testStaticMockWithMoMoreInteractions() {
7376
}
7477

7578
@Test(expected = NoInteractionsWanted.class)
76-
public void testStaticMockWithMoMoreInteractionsFailed() {
79+
public void testStaticMockWithNoMoreInteractionsFailed() {
7780
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
7881
dummy.when(Dummy::foo).thenReturn("bar");
7982
assertEquals("bar", Dummy.foo());
@@ -171,12 +174,27 @@ public void testStaticMockVoid() {
171174
try (MockedStatic<Dummy> dummy = Mockito.mockStatic(Dummy.class)) {
172175
Dummy.fooVoid("bar");
173176
assertNull(Dummy.var1);
174-
dummy.verify(()->Dummy.fooVoid("bar"));
177+
dummy.verify(() -> Dummy.fooVoid("bar"));
175178
}
176179
Dummy.fooVoid("bar");
177180
assertEquals("bar", Dummy.var1);
178181
}
179182

183+
@Test
184+
public void testStaticMockMustUseValidMatchers() {
185+
try (MockedStatic<Dummy> mockedClass = Mockito.mockStatic(Dummy.class)) {
186+
assertThatThrownBy(
187+
new ThrowableAssert.ThrowingCallable() {
188+
public void call() {
189+
mockedClass.when(() -> Dummy.fooVoid("foo", any())).thenReturn(null);
190+
}
191+
})
192+
.hasMessageContaining("Invalid use of argument matchers!");
193+
194+
Dummy.fooVoid("foo", "bar");
195+
}
196+
}
197+
180198
static class Dummy {
181199

182200
static String var1 = null;
@@ -188,5 +206,9 @@ static String foo() {
188206
static void fooVoid(String var2) {
189207
var1 = var2;
190208
}
209+
210+
static void fooVoid(String var2, String var3) {
211+
var1 = var2;
212+
}
191213
}
192214
}

0 commit comments

Comments
 (0)