I am writing a test using Junit + Mockito + Powermock.
I have a class like following which I want to test:
public class MyUtils {
public static Object method1() {} //I want to mock this only
public static void method2() {} //I want to keep this as is during my test.
public static void method3() {} //I want to keep this as is during my test.
}
I want to mock only method1
but not method2
or method3
.
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyUtils.class)
public class MyTest {
@Before
public void setUpBeforeClass() throws Exception {
PowerMockito.mockStatic(MyUtils.class);
}
@Test
public void test1() throws Exception {
when(MyUtils.method1()).thenReturn(something);
MyUtils.method3(); //method3 is getting mocked with an empty implementation by PowerMockito
}
...
}
Can I have some methods mocked and some not be mocked i.e. they keep their original implementation during the test? Is this possible with Mockito + Powermock?
My test may not look very elegant but I have simplified my usecase before posting here.
Thank you.