1

I have the following code:

public void method(String a, Long b,  Function<Long, List<String> someFunc){...}

This method, I want to stub.

Mockito.when(myClass.method(anyString(), anyLong(), any...??????).then(...)

How do I match a function in here? I have tried any(Function.class), but obviously this doesn't work.

Thanks

Christian
  • 6,961
  • 10
  • 54
  • 82

1 Answers1

0

You can use the any() method from the ArgumentMatchers class like this:

Class under test:

import java.util.List;
import java.util.function.Function;

public class TestedClass {
    
    public void method(String a, Long b, Function<Long, List<String>> someFunc) {
        // ...
    }
}

Test:

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class TestedClassTest {
    
    @Test
    void test() {
        TestedClass testedClass = Mockito.mock(TestedClass.class);
        Mockito.doNothing().when(testedClass).method(anyString(), anyLong(), any()); // use any() without parameters and let the compiler find the generic parameters for you
        
        testedClass.method("string", 42L, null);
    }
    
}

This answer explains the usage and how it works quite well.

Tobias
  • 2,547
  • 3
  • 14
  • 29