Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to match on a call-by-name argument of a Mockito mock object in Specs?

Tags:

scala

specs

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.

like image 573
Wilfred Springer Avatar asked Mar 01 '12 07:03

Wilfred Springer


People also ask

What is argument matcher in Mockito?

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.

What is EQ Mockito?

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.

What is difference between @mock and Mockito mock?

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.

What are the different approaches that we can use to mock objects using Mockito?

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.


1 Answers

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.

like image 122
Eric Avatar answered Nov 12 '22 16:11

Eric