forked from mockito/mockito
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mockito#2616 wrong stub for nested static
- Loading branch information
1 parent
ec27830
commit b62c317
Showing
2 changed files
with
73 additions
and
12 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
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 int outer() { | ||
return inner() + 1; | ||
} | ||
|
||
static int inner() { | ||
throw new NullPointerException(); | ||
} | ||
} | ||
|
||
@Test | ||
public void keep_ongoing_stub_when_exception() { | ||
try (MockedStatic<StaticWithException> mock = mockStatic(StaticWithException.class, CALLS_REAL_METHODS)) { | ||
mock.when(StaticWithException::outer).thenReturn(1); | ||
assertEquals(1, StaticWithException.outer()); | ||
} | ||
} | ||
} |