Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Mockito equivalent of expect().andReturn().times()

I've been experimenting the Mockito equivalent of the

EasyMock.expect(someMethod()).andReturn(someMockObject).times(n);

but I can't figure it out.

A little help on this, please?

Thanks in advance.

like image 993
leinra Avatar asked Jan 18 '13 03:01

leinra


People also ask

What is andReturn?

The andReturn() method defines the return value of this method for the specified method parameters. The times() method defines how often the Mock object will be called. The replay() method is called to make the Mock object available. // setup the mock object expect(calcMethod.

What is when thenReturn in Mockito?

Mockito Behavior Verification To add a behavior to the mocked class when() and thenReturn() functions are used. It means that when the mock object (addService) is called for add method with (num1, num2) parameters, then it returns the value stored in the expected variable.

Which is better JMockit or Mockito?

JMockit will be the chosen option for its fixed-always-the-same structure. Mockito is more or less THE most known so that the community will be bigger. Having to call replay every time you want to use a mock is a clear no-go, so we'll put a minus one for EasyMock. Consistency/simplicity is also important for me.


1 Answers

when(myObject.someMethod()).thenReturn(someMockObject);
// run test
verify(myObject, times(n)).someMethod();

See the documentation for more conversion examples.

like image 67
Alex DiCarlo Avatar answered Oct 10 '22 01:10

Alex DiCarlo