0

我正在测试一个名为的类实例server,我正在使用部分模拟,如下所示:

new Expectations(server) {{
    server.readPortNumber(withInstanceOf(File.class));
    result = new FileNotFoundException();
    times = 300;
}}

这适用于前 300 个调用。但是,301 调用应该会成功,所以我期待这样的工作:

new Expectations(server) {{
    server.readPortNumber(withInstanceOf(File.class));
    result = new FileNotFoundException();
    times = 300;
    result = 100;
    times = 1;
}}

但事实并非如此。readPortNumber在它的第一次调用中返回100,显示值被覆盖。

如何使用times关键字指定结果链?

4

1 回答 1

0

我能够使用以下方法找到答案Delegate

new Expectations(server) {{
    server.readPortNumber(withInstanceOf(File.class));
    result = new FileNotFoundException();
    times = 301;
    result = new Delegate() {
        int n_calls = 0;

        int delegate() throws FileNotFoundException {
            n_calls++;
            if (n_calls <= 300) {
                throw new FileNotFoundException();
            } else {
                return 100;
            }
        }
    };
}}

不确定是否有更好的解决方案,比这更简洁。

于 2018-05-30T12:50:14.430 回答