【问题标题】:How to capture varargs如何捕获可变参数
【发布时间】:2016-09-30 11:57:11
【问题描述】:

我有静态方法,我想捕获 varargs

Executor ex = new Executor();
ex.execute(String nodeName, boolean status, Property ... properties);

ArgumentCaptor<Property> propertyCaptor = ArgumentCaptor.forClass(Property.class);
verify(ex).execute(anyString(), anyBoolean(), propertyCaptor.capture);

propertyCaptor.getValue() - 不起作用????

【问题讨论】:

  • Property... 的类是 Property[].class

标签: java unit-testing junit powermock


【解决方案1】:

我不确定您到底要测试什么,但以下方法有效:

class SpecialExecutor implements Executor {
        @Override
        public void execute(Runnable command) {

        }

        public void execute(String nodeName, boolean status, Property... properties) {

        }
    };

    @Test
    public void test() {
        SpecialExecutor ex = new SpecialExecutor();

        ArgumentCaptor<Property> propertyCaptor = ArgumentCaptor.forClass(Property.class);
        verify(ex).execute(anyString(), anyBoolean(), any(Property[].class));
    }

【讨论】:

  • 你运行了这段代码吗?如果您有 case: one varargs 参数,则此方法有效,但如果您有 > 1 个参数,则此方法无效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 2012-12-31
  • 2013-11-29
相关资源
最近更新 更多