我编写单元测试来测试一些dll加载。在我的测试用例中,我希望我的静态方法Utils.loadLibrary始终返回一个有效的对象。在我的测试用例中没有问题但是当我在我的测试用例中调用一个新方法时,模拟没有用。在我的单元测试运行时是否可以模拟整个方法?
@RunWith(PowerMockRunner.class)
@PrepareForTest(Utils.class)
public class ControlTest {
private static final String ROOT = "Root\\";
@Test
public void testCapturing() {
PowerMockito.mockStatic(Utils.class);
Mockito.when(Utils.loadLibrary(Matchers.anyString(),Matchers.any())).thenReturn(new Object());
final Object o = Utils.loadLibrary("", null);
final String[] paths = new String[2];
final String scriptPathLeft = this.getClass().getResource("folder/Script_0.txt").getPath();
final String scriptPathRight = this.getClass().getResource("folder/Script_1.txt").getPath();
paths[0] = scriptPathLeft;
paths[1] = scriptPathRight;
final Params params = new Params(ROOT);
final String PathLeft = ROOT + "\\Folder";
final String PathRight = ROOT + "\\Folder";
final File FileLeft = new File(PathLeft);
final File FileRight = new File(PathRight);
nikonFileLeft.mkdirs();
nikonFileRight.mkdirs();
final Control control = new Control(nikonImagePathLeft, nikonImagePathRight, params);
for (int i = 0; i < paths.length; i++) {
if (!paths[i].isEmpty()) {
control .prepareScriptExecution(paths[i], nikonImagePathLeft, i == 1);
}
}
}
}
在方法prepareScriptExecution中,我调用了Utils.LoadLibrary,并希望此方法返回我的模拟对象而不是null。我不想在我的测试用例中加载dll。