模拟Runtime.getRuntime()?

Ric*_*ich 15 java junit unit-testing easymock runtime.exec

任何人都可以提出任何有关如何最好地使用EasyMock预期来电的建议Runtime.getRuntime().exec(xxx)吗?

我可以将调用移动到另一个实现接口的类中的方法,但不希望在理想的世界中.

interface RuntimeWrapper {
    ProcessWrapper execute(String command) throws IOException;
}

interface ProcessWrapper {
    int waitFor() throws InterruptedException;
}
Run Code Online (Sandbox Code Playgroud)

我想知道是否有人有任何其他建议?

Boz*_*zho 14

你的班级不应该打电话Runtime.getRuntime().它应该期望将a Runtime设置为其依赖关系,并使用它.然后在测试中,您可以轻松提供模拟并将其设置为依赖项.

作为旁注,我建议观看OO设计的可测试性讲座.

更新:我没有看到私有构造函数.您可以尝试使用java字节码检测以添加另一个构造函数或使构造函数公开,但这可能也是不可能的(如果该类有一些限制).

所以你的选择是制作一个包装器(正如你在问题中所建议的那样),并遵循依赖注入方法.


Mic*_*mlk 6

上面的Bozho是IMO的正确解决方案.但它不是唯一的解决方案.您可以使用PowerMockJMockIt.

使用PowerMock:

package playtest;

public class UsesRuntime {
    public void run() throws Exception {
        Runtime rt = Runtime.getRuntime();
        rt.exec("notepad");
    }
}


package playtest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.legacy.PowerMockRunner;

import static org.powermock.api.easymock.PowerMock.*;
import static org.easymock.EasyMock.expect;

@RunWith(PowerMockRunner.class)
@PrepareForTest( { UsesRuntime.class })
public class TestUsesRuntime {

    @Test
    public void test() throws Exception {
        mockStatic(Runtime.class);
        Runtime mockedRuntime = createMock(Runtime.class);

        expect(Runtime.getRuntime()).andReturn(mockedRuntime);

        expect(mockedRuntime.exec("notepad")).andReturn(null);

        replay(Runtime.class, mockedRuntime);

        UsesRuntime sut = new UsesRuntime();
        sut.run();
    }
}
Run Code Online (Sandbox Code Playgroud)