UserCodeException extends RuntimeException { public static RuntimeException wrapIf(boolean condition, Throwable t) { if (condition) { return wrap(t); } if (t instanceof RuntimeException) { return (RuntimeException) t; } return new RuntimeException(t); } private  UserCodeException(Throwable t); static UserCodeException wrap(Throwable t); static RuntimeException wrapIf(boolean condition, Throwable t);  }
@Test public void testWrapIfOnlyWrapsWhenTrue() { IOException cause = new IOException(); RuntimeException wrapped = UserCodeException.wrapIf(true, cause); assertThat(wrapped, is(instanceOf(UserCodeException.class))); }
@Test public void testWrapIfReturnsRuntimeExceptionWhenFalse() { IOException cause = new IOException(); RuntimeException wrapped = UserCodeException.wrapIf(false, cause); assertThat(wrapped, is(not(instanceOf(UserCodeException.class)))); assertEquals(cause, wrapped.getCause()); }
@Test public void testWrapIfReturnsSourceRuntimeExceptionWhenFalse() { RuntimeException runtimeException = new RuntimeException("oh noes!"); RuntimeException wrapped = UserCodeException.wrapIf(false, runtimeException); assertEquals(runtimeException, wrapped); }