I am testing the interaction between one object, and another object with some methods that have call-by-name arguments. However, I can't figure out how to create an argument matcher for that call-by-name argument.
Let's say that this is the signature of the mocked object:
def fn(arg1: => String, arg2: Int): Any
Then what I really want to do is test if that method is called with a known second argument. I don't even care all that much about the first argument, but having a way to properly test that as well would be a bonus.
This doesn't work:
there was one(mock) fn(any[()=>String], eq(12))
nor this:
there was one(mock) fn(any[Function0[String]], eq(12))
and this doesn't even compile:
there was one(mock) fn(any[=>String], eq(12))
... which obviously is to be expected.
Argument matchers are mainly used for performing flexible verification and stubbing in Mockito. It extends ArgumentMatchers class to access all the matcher functions. Mockito uses equal() as a legacy method for verification and matching of argument values.
Mockito Argument Matcher - eq() When we use argument matchers, then all the arguments should use matchers. If we want to use a specific value for an argument, then we can use eq() method. when(mockFoo. bool(eq("false"), anyInt(), any(Object.
Using an annotation ( @Mock ) is usually considered "cleaner", as you don't fill up your code with boilerplate assignments that all look the same. Note that in order to use the @Mock annotation, your test class should be annotated with @RunWith(MockitoJUnitRunner. class) or contain a call to MockitoAnnotations.
The Mockito framework provides a variety of methods such as mock(), verify(), when(), etc., used to test Java applications. Using these predefined methods makes testing very easy.
I've just added some support in specs2 for byname arguments and functions/partial function arguments. Please try out the latest specs2-1.9-SNAPSHOT and create an issue on GitHub if that doesn't work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With