Java Mock throw an exception, then return a value?

Viewed 11467

I am using JUnit 4 and Mockito 2. I am trying to mock a situation where the mocked function returns an exception the first time it is called, and on the subsequent call a valid value is returned. I tried simply having a thenThrow() followed by a thenReturn(), but that is not the correct method apparently

when(stmt.executeUpdate()).thenThrow(new SQLException("I have failed."));
when(stmt.executeUpdate()).thenReturn(1);
sut.updateValue("1");
verify(dbc).rollback();
sut.updateValue("2");
verify(dbc).commit();

Both calls, however, result in a call to rollback(), which is in the catch statement.

3 Answers
Related