使用PowerMockito模拟java.lang.Runtime [英] Mock java.lang.Runtime with PowerMockito

查看:279
本文介绍了使用PowerMockito模拟java.lang.Runtime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想为像

public static void startProgram() {
    process = Runtime.getRuntime().exec(command, null, file);
}

由于某些原因我不想注入运行时对象,所以我想要存根getRuntime方法,它返回一个Runtime mock ...我试过这样:

I don't want to inject the runtime object for some reasons, so I wanted to stub the getRuntime method that it returns a Runtime mock... I tried it this way:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Runtime.class)
public class ProgramTest {

    @Test
    public void testStartProgram() {
        Runtime mockedRuntime = PowerMockito.mock(Runtime.class);

        PowerMockito.mockStatic(Runtime.class);
        Mockito.when(Runtime.getRuntime()).thenReturn(mockedRuntime);

        ... //test
    }
}

但这不起作用。实际上似乎没有任何东西被嘲笑。在测试中使用正常的Runtime对象。

But this doesn't work. Actually nothing seems to be mocked. In the test the normal Runtime object is used.

任何人都知道为什么这不起作用和/或它是如何工作的?

Anyone any idea why this doesn't work and/or how it works?

由于这个迷你示例似乎没有重现问题,这里是完整的测试代码:
测试方法(缩短)

As this mini example seems not to reproduce the problem here is the full test code: method to test (shortened)

public static synchronized long startProgram(String workspace) {
    // Here happens someting with Settings which is mocked properly
    File file = new File(workspace);
    try {
        process = Runtime.getRuntime().exec(command, null, file);
    } catch (IOException e) {
        throw e;
    }
    return 0L;
}

和测试:

@Test
public void testStartProgram() {
    PowerMockito.mockStatic(Settings.class);
    Mockito.when(Settings.get("timeout")).thenReturn("42");

    Runtime mockedRuntime = Mockito.mock(Runtime.class);
    // Runtime mockedRuntime = PowerMockito.mock(Runtime.class); - no difference
    Process mockedProcess = Mockito.mock(Process.class);

    Mockito.when(mockedRuntime.exec(Mockito.any(String[].class), Mockito.any(String[].class),
                    Mockito.any(File.class))).thenReturn(mockedProcess);

    PowerMockito.mockStatic(Runtime.class);
    Mockito.when(Runtime.getRuntime()).thenReturn(mockedRuntime);

    startProgram("doesnt matter");
}

然后,在测试中,对Runtime.getRuntime()的调用没有'带来模拟,这就是抛出IOException的原因,因为String不是目录......

Then, in the test, the call to Runtime.getRuntime() doesn't bring the mock and that's why an IOException is thrown because the String is no directory...

推荐答案

我的评论不好道歉,我有一个内心的测试,这就是为什么我没有遇到麻烦。然后我意识到这一点并看到你的 @PrepareForTest(Runtime.class),它应该是 @PrepareForTest(MyClass.class) (用您拥有的任何名称替换MyClass)因为运行时系统类。您可以在此处阅读有关此 的更多信息并查找更多示例这里

My bad in the comments, apologies, I had an inner class for the test and that's why I had no trouble. I then realized this and saw your @PrepareForTest(Runtime.class) which should read @PrepareForTest(MyClass.class) (replace MyClass with whatever name you have) because Runtime is a system class. You can read more about this here and find more examples here.

这篇关于使用PowerMockito模拟java.lang.Runtime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆