如何捕获varargs

时间:2016-09-30 11:57:12

标签: java unit-testing junit powermock

我有静态方法,我想捕获 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() - 无效????

1 个答案:

答案 0 :(得分: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));
    }