使用带有Junit的EasyMock忽略方法/ void方法

时间:2013-10-24 10:54:34

标签: java junit junit4 easymock

"添加了更多详细信息"

我想模拟某种虚空方法,但我不太确定如何。我读到了关于EasyMock但我不知道该怎么办,当它是一个无效方法时,这是我的主要课程;

主要课程

public class Main {
    Updater updater = new Updater(main.getID(), main.getName(),....);

    try {
        updater.updateContent(dir);
    }

我想模仿updater.updateContent(dir);,以便我可以跳过尝试

更新程序类

private String outD;

public void updateContent(final String outDir) throws Exception {


    outD = outDir;
    if (...) {
        ....;
    }

}

...私有空方法

到目前为止,这是我的测试课程,

public class MainTest {


    @Before
    public void setUp() {

    }

    @Test
    public void testMain() { 


try {


        try {
            Updater updater = EasyMock.createNiceMock(Updater.class);
            updater.updateContent("/out");
            EasyMock.expectLastCall().andThrow(new RuntimeException()); 

            EasyMock.replay(updater);

            updater.updateContent("/out");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}


}
    }

(编辑)谢谢。

1 个答案:

答案 0 :(得分:1)

对于返回void的方法,您必须以这种方式记录行为:

 Updater updater = EasyMock.createNiceMock(Updater.class);
 updater.updateContent("someDir"); // invoke the easy mock proxy and
 // after invoking it record the behaviour.
 EasyMock.expectLastCall().andThrow(new RuntimeException()); // for example

 EasyMock.replay(updater);

 updater.updateContent("someDir"); // will throw the RuntimeException as recorded

期待您拥有以下Main课程

public class Main {
    private Updater updater;

    private int updatedContentCount; // introduced for the example

    public Main(Updater updater) {
        this.updater = updater;
    }

    public void updateContent() {
        try {
            updater.updateContent("/out");
            updatedContentCount++;
        } catch (Exception e) {
            // skip for this example - normally you should handle this
        }
    }

    public int getUpdatedContentCount() {
        return updatedContentCount;
    }

}

并且您的更新程序的API看起来像这样

public class Updater {

    public void updateContent(String dir) throws Exception {
        // do something
    }
}

然后对Main类的测试将是这样的:

public class MainTest {

    private Updater updater;
    private Main main;

    @Before
    public void setUp() {
        updater = EasyMock.createNiceMock(Updater.class);
        main = new Main(updater);
    }

    @Test
    public void testUpdateCountOnException() throws Exception {
        updater.updateContent("/out");
        EasyMock.expectLastCall().andThrow(new RuntimeException());
        EasyMock.replay(updater);
        main.updateContent();
        int updatedContentCount = main.getUpdatedContentCount();
        Assert.assertEquals(
                "Updated count must not have been increased on exception", 0,
                updatedContentCount);
    }
}

MainTest测试是否在Updater的例外情况下正确处理了updateCount。