【问题标题】:Ignore methods/void methods using EasyMock with Junit使用带有 Junit 的 EasyMock 忽略方法/无效方法
【发布时间】:2013-10-24 10:54:33
【问题描述】:

“添加了更多细节”

我想模拟某种 void 方法,但我不太确定如何去做。我阅读了有关 EasyMock 的信息,但当它是一个 void 方法时我不知道该怎么做,这是我的主要课程;

主类

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

    try {
        updater.updateContent(dir);
    }

我想模拟 updater.updateContent(dir); 以便我可以跳过 try

更新程序类

private String outD;

public void updateContent(final String outDir) throws Exception {


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

}

... 私有 void 方法

这是我目前的测试课,

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();
    }

}


}
    }

(已编辑)谢谢。

【问题讨论】:

  • “跳过尝试”是什么意思?
  • @dkatzel 表示我不想执行 updater.updateContent(dir);

标签: java junit junit4 easymock


【解决方案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 测试 updateCount 是否在 Updater 的异常上得到正确处理。

【讨论】:

  • 我添加了你给出的内容,请看看有什么问题
  • 目前我无法弄清楚你的课程是如何运作的。不过,我会更新我的答案并向您展示测试如何进行。
  • 当我使用 junit 运行它时,它仍然会使用 updateContent 方法
  • 我添加的代码是我认为你想要做的假设,因为你提供的代码是不够的。因此我添加了代码来展示单元测试的基本原理。
  • 是的,我现在就去试试。。我也更新了主类,也来看看吧:)
猜你喜欢
  • 2012-05-22
  • 2010-10-25
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 2020-07-08
相关资源
最近更新 更多