Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix nested static mock stubs (#2685)
This also fixes the `WrongTypeOfReturnValue` issue for spy stubs. Fixes #2616 Co-authored-by: yushouqiu <yushouqiu@xiaomi.com>
- Loading branch information
1 parent
512ee39
commit 807b121
Showing
3 changed files
with
78 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
subprojects/inline/src/test/java/org/mockitoinline/bugs/OngoingStubShiftTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright (c) 2007 Mockito contributors | ||
* This program is made available under the terms of the MIT License. | ||
*/ | ||
package org.mockitoinline.bugs; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
import static org.mockito.Mockito.CALLS_REAL_METHODS; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
public class OngoingStubShiftTest { | ||
|
||
private static class StaticInt { | ||
static int getInt() { | ||
return 1; | ||
} | ||
} | ||
|
||
private static class StaticStr { | ||
static String getStr() { | ||
return Integer.toString(StaticInt.getInt()); | ||
} | ||
} | ||
|
||
@Test | ||
public void keep_ongoing_stub_when_spy() { | ||
try (MockedStatic<StaticInt> mockInt = mockStatic(StaticInt.class); | ||
MockedStatic<StaticStr> mockStr = mockStatic(StaticStr.class, CALLS_REAL_METHODS)) { | ||
|
||
mockStr.when(StaticStr::getStr).thenReturn("1"); | ||
assertEquals("1", StaticStr.getStr()); | ||
} | ||
} | ||
|
||
private static class StaticWithException { | ||
static String getString() { | ||
return Integer.toString(getInt()); | ||
} | ||
|
||
static int getInt() { | ||
throw new NullPointerException(); | ||
} | ||
} | ||
|
||
@Test | ||
public void keep_ongoing_stub_when_exception() { | ||
try (MockedStatic<StaticWithException> mock = mockStatic(StaticWithException.class, CALLS_REAL_METHODS)) { | ||
mock.when(StaticWithException::getString).thenReturn("1"); | ||
assertEquals("1", StaticWithException.getString()); | ||
} | ||
} | ||
} |