@Test public void retries_no_exceptions() throws Exception { final String value = "foo"; when(operation.execute()).thenReturn(value); final Object result = retrier.execute(operation); assertNotNull(result); assertEquals(value, result); }
@SuppressWarnings("unchecked") @Test(expected = IllegalStateException.class) public void reties_throw_some_exceptions() throws Exception { retrier.setCatchAndThrow(new Class[] {IllegalStateException.class}); when(operation.execute()) .thenThrow(new Exception()) .thenThrow(new IllegalStateException()) .thenThrow(new Exception()); retrier.execute(operation); }
@SuppressWarnings("unchecked") @Test(expected = NullPointerException.class) public void reties_throw_exceptions() throws Exception { retrier.setCatchAndThrow(new Class[] {NullPointerException.class}); when(operation.execute()) .thenThrow(new Exception("ow")) .thenThrow(new Exception("ow")) .thenThrow(new NullPointerException()); retrier.execute(operation); }
@Test public void reties_exceptions_after_success() throws Exception { final String value = "foo"; when(operation.execute()) .thenThrow(new Exception("ow")) .thenReturn(value) .thenThrow(new Exception("ow")); final String result = retrier.execute(operation); assertNotNull(result); assertEquals(value, result); }
@Test public void reties_all_exceptions() throws Exception { when(operation.execute()) .thenThrow(new Exception("ow")) .thenThrow(new Exception("ow")) .thenThrow(new Exception("ow")); try { retrier.execute(operation); fail("Expected exception not thrown!"); } catch (final Exception e) { assertMaxExceededException(e, 3); } }