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

后端 未结 3 1556
面向向阳花
面向向阳花 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:00

    In your particular case, getProperty(String) calls getProperty(String, String) internally.

    public String getProperty(String key) {
        /*
         * getProperty(String, String) is called anyway.
         * Why not simply verify the occurrence of that?
         */
        return getProperty(key, null);
    }
    

    Simply verifying the second method would be equivalent to verifying the occurrence of either one or the other at least once.

    Mockito.verify(foo, atLeastOnce()).getProperty(anyString(), anyString());
    

提交回复
热议问题