'JUnit Mockito: Testing a Static Method and Calling Another Stubbed Static Method Inside Not Working
class A {
public static int f1() {
return 1;
}
public static int f2() {
return A.f1();
}
}
class ATest {
@Test
void testF2() {
try (MockedStatic<A> aStatic = Mockito.mockStatic(A.class)) {
aStatic.when(A::f1).thenReturn(2);
int ret = A.f2(); // getting 0 here
assertEquals(ret, 2);
} catch(Exception e) {
}
}
}
In the testF2 I want to test static function A::f2().
And it internally calls another static function A::f1().
I did stub A::f1() to return 2 using "MockedStatic" and "when" way.
But it's not working, it's returning 0.
How to solve it?
Solution 1:[1]
I think you miss to specify a mock behavior:
class ATest {
@Test
void testF2() {
try (MockedStatic<A> aStatic = Mockito.mockStatic(A.class)) {
aStatic.when(A::f1).thenReturn(2);
aStatic.when(A::f2).thenReturn(A.f1()); // <- added this
int ret = A.f2(); // getting 0 here
Assertions.assertEquals(ret, 2);
} catch (Exception e) {
}
}
}
by telling the mock what to do when A.f2()
is invoked, test runs fine.
Update:
Mocks do what you tell them, if you don't tell what to do when a method is invoked they do nothing, that's why you have to mock f2
too.
You want to test A
, then mock it is not your friend. I normally use a Mockito.spy()
to partially mock my subject under test .You want to mock f1
but test f2
, I don't think spy
applies here because there is no instance to spy..
I suggest you to rearrange A
avoiding static methods if possible or using parameters you can mock.
Solution 2:[2]
When you mock a class with static methods, all static methods are mocked. If you only want to mock the behavior of only 1 method, you have to add Mockito.CALLS_REAL_METHODS argument to Mockito.mockStatic() as you can see in the following example.
@Test
void testF2() {
try (MockedStatic<A> aStatic = Mockito.mockStatic(A.class, Mockito.CALLS_REAL_METHODS)) {
aStatic.when(A::f1).thenReturn(2);
int ret = A.f2(); // getting 2 here
Assert.assertEquals(2, ret); // (expected, result)
} catch(Exception e) {
}
}
This way only the f1 method invocation is mocked but f2 invocation calls the real code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Juanvi Bolufer |