2016-09-30 1 views
2

I statische Methode haben, und ich will varargsWie erfassen 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() erfassen - nicht funktioniert ????

+1

Die Klasse für 'Property ...' ist 'Property []. Class' –

Antwort

1

Ich bin mir nicht sicher, was genau Sie testen wollen, aber die folgenden Werke:

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)); 
    } 
+0

haben Sie diesen Code ausgeführt? Diese Arbeit, wenn Sie Fall haben: ein varargs Parameter, aber das funktioniert nicht, wenn Sie> 1 Parameter haben. –

0

Im Falle eines varargs Sie benötigen getAllValues() statt getValue() wie das Beispiel der javadoc von ArgumentCaptor zu verwenden :

//capturing varargs: 
ArgumentCaptor<Person> varArgs = ArgumentCaptor.forClass(Person.class); 
verify(mock).varArgMethod(varArgs.capture()); 
List expected = asList(new Person("John"), new Person("Jane")); 
assertEquals(expected, varArgs.getAllValues()); 

Also in Ihrem Fall wird es sein:

ArgumentCaptor<Property> propertyCaptor = ArgumentCaptor.forClass(Property.class); 
verify(ex).execute(anyString(), anyBoolean(), propertyCaptor.capture()); 
assertEquals(expected, propertyCaptor.getAllValues()); 
+0

es funktioniert nicht! –