How can I verify that one of two methods was called using Mockito?

后端 未结 3 1580
面向向阳花
面向向阳花 2021-01-04 01:18

Suppose I have a class with two methods where I don\'t care which is called...

public class Foo {
    public String getProperty(String key) {
        return          


        
3条回答
  •  误落风尘
    2021-01-04 02:11

    You could use atLeast(0) in combination with ArgumentCaptor:

    ArgumentCaptor propertyKeyCaptor = ArgumentCaptor.forClass(String.class);
    Mockito.verify(foo, atLeast(0)).getProperty(propertyKeyCaptor.capture(), anyString());
    
    ArgumentCaptor propertyKeyCaptor2 = ArgumentCaptor.forClass(String.class);
    Mockito.verify(foo, atLeast(0)).getProperty(propertyKeyCaptor2.capture());
    
    List propertyKeyValues = propertyKeyCaptor.getAllValues();
    List propertyKeyValues2 = propertyKeyCaptor2.getAllValues();
    
    assertTrue(!propertyKeyValues.isEmpty() || !propertyKeyValues2.isEmpty()); //JUnit assert -- modify for whatever testing framework you're using
    

提交回复
热议问题