Mockito.when().thenXYZ
Mockito.when().thenReturn()
It is used to do stubbing on the method.
So, after stubbing any method, any number of times the method call happens it returns the same.
Unless it is again stubbed and changed.
Example:
@Test
public void test_When_ThenReturn(){
ArrayList<String> list = Mockito.mock(ArrayList.class);
// stubbing
Mockito.when(list.get(0)).thenReturn("Good");
System.out.println(list.get(0));
}
Here it will return "Good" whenever list.get(0) happenes.
Mockito.when().thenThrow()
-------
It is used to do stubbing on the method and throw exception in call result.
So, after stubbing any method, any number of times the method call happens it throws the mentioned exception.
Unless it is again stubbed and changed.
Example:
@Test
public void test_When_ThenThrow(){
ArrayList<String> list = Mockito.mock(ArrayList.class);
Mockito.when(list.get(1)).thenThrow(new RuntimeException());
System.out.println(list.get(1));
}
Here it will throw RuntimeException whenever a call list.get(1) happens.