我要在 Util 类中模拟一个名为toBeMockedFunction的静态函数。此方法是从toBeUnitTested调用的,它是一个公共静态 void 方法。我想对 BeMocked 函数什么都不做。我尝试了许多部分模拟和存根的方法(发布了这样的 2 的片段)并且无法成功。
请提出我做错了什么。
public class Util {
// Some code
public static void toBeUnitTested(CustomObject cb, CustomObject1 cb1, List<CustomObject2> rows, boolean delete) {
// some code
toBeMockedFunction(cb, "test", "test");
}
public static CustomObject toBeMockedFunction(CustomObject cb, String str1) {
// some code
}
}
下面是我的junit类
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Util.class})
public class UtilTest {
@Test
public void Test1() {
PowerMockito.spy(Util.class);
//mock toBeMocked function and make it do nothing
PowerMockito.when(PowerMockito.spy(Util.toBeMockedFunction((CustomObject)Mockito.anyObject(), Mockito.anyString()))).thenReturn(null);
Util.toBeUnitTested(cb, "test", "test");
}
}
方法2
PowerMockito.mockStatic(Util.class); PowerMockito.when(Util.toBeUnitTested((CustomObject)Mockito.anyObject(),Mockito.anyString())).thenCallRealMethod(); Util.toBeUnitTested(cb, "test", "test");