3

I just need to know

1) when I call when() in Mockito, it is actually executing the mockedObject method behaviour or not?

2) what If when I return a wrong value in when().thenReturn() like below. The test case will be "false" or will it return 50 instead of 30?

 when(mockedObject.add(10,20)).thenReturn(50);

3) why should I need to use thenReturn() mandatory, even when the mocked object method is void? ( it's giving error when I didn't use thenReturn().

adds functionality to a mock object using the when() method.

@Test
public void testAdd(){
 when(mockedObject.add(20.0,30.0)).thenReturn(50.00);
}

4) If I call

  verify(mockedObject,times(1)).add(20.0, 30.0);

It checks that the methods are called on a mocked object or not with the same parameter and check the call to the method as only 1 time, right? So can I use when() to add a functionality to the mocked object method without using verify() or it actually requires to invoke verify() after calling when() method

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
anand krish
  • 4,281
  • 4
  • 44
  • 47

2 Answers2

1

1) No, unless you used @Spy instead of @Mock. Thats why you can mock even an interface.

2) It will return the default value depending on the type of return value (for example will return 0 if return type is Integer).

3) If you used @Mock then you do not need to use when.thenReturn for void methods of that mock. The implementation simply wont be called.

4) when is used in the Arrange part of your test before you invoke the method under test. verify is used after the invocation in the assertion part. The use of both of them in one test case is not mandatory. You could use only when, only verify of both in appropriate places.

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • 1) you said No for 1 question, I fell it is YES. when() will execute mockedObject method behaviour only. When you use @spy, then only actual method of real object will get called I think. check this https://www.tutorialspoint.com/mockito/mockito_spying.htm – anand krish Sep 29 '17 at 15:16
  • When you do not use when.thenRetrun for a Mocked object then the implementation wont be called you will get the default return value. If you do not use doReturn.when.then on a Spied object, then a real method will be called – Maciej Kowalski Sep 29 '17 at 15:20
1

1) When you are using mocks, you are creating a simulation of the class in place of the actual class. Since the object so created doesn't understand what to do when a certain method of the class is called, you need to use when/then to simulate the behavior. The answer to this is NO since you are specifying the behavior that needs to be executed in your test case. Real behavior will be executed when you make an object of the class using the new constructor or when you use @Spy to replicate actual behavior wherever possible.

2) When you specify the below line in your test case, you are instructing the runner to return 50 whenever add() is called with the arguments 10, 20.

when(mockedObject.add(10,20)).thenReturn(50);

So yes if you write an assert as mentioned below your test will pass since you mentioned that you need the method to return 50 and not the default value:

assertEquals(50, mockedObject.add(10,20));

3) You cannot write when thenReturn for void methods. Mockito will throw error saying 'The method when(T) in the type Mockito is not applicable for the arguments (void)'. For void methods, max you can do is verify if a method has been called or not. I believe you are testing a method that does return something and is not void. Please do not confuse return type of test case with return types of methods inside them.

You can also use doNothing() or doAnswer() with void methods in case you need to override something specific in those.

4) It checks that the methods is called on mocked object or not with same parameter and check the call to the method as only 1 times, right? - Right

verify as I mentioned in 3) is mostly used for testing void methods and is a value add in other tests where assertions would be enough most of the times. You cannot write an assert when the method returns nothing. That's where verify comes into picture.