when(mockObj.method(param1, param2)).thenReturn(1);
when(mockObj.method(param1, param2)).thenReturn(2);
When there are conflicting statements to return the value from a method with same argument list in a mocked object, I observed that the recent when/thenReturn will be returned. So, the below statement will be true.
assertEquals(2, mockObj.method(param1, param2));
When there are conflicting statements to throw exceptions, the behavior is not the same as above. For example,
@Test(expected = ExceptionTwo.class)
public void testMethod() {
when(mockObj.method(param1, param2)).thenThrow(ExceptionOne.class);
when(mockObj.method(param1, param2)).thenThrow(ExceptionTwo.class);
mockObj.method(param1, param2);
}
This test case failed. Any explanation would be helpful.