示例#1
0
  @Test
  public void getAThrowingSupplierUnchecked() {
    assertThat(getUnchecked(() -> 42), is(42));

    Exception e = new Exception();
    try {
      getUnchecked(
          () -> {
            throw e;
          });
    } catch (RuntimeException ex) {
      assertThat(ex.getCause(), sameInstance(e));
      return;
    }
    fail("Should throw exception");
  }
示例#2
0
  @Test
  public void applyAThrowingFunctionUnchecked() {
    assertThat(applyUnchecked(Math::round, 4.6f), is(5));
    assertThat(applyUnchecked(n -> n, null), nullValue());
    assertThat(applyUnchecked(n -> n, Optional.empty()), is(Optional.empty()));

    Exception e = new Exception();
    try {
      applyUnchecked(
          t -> {
            throw e;
          },
          "anything");
    } catch (RuntimeException ex) {
      assertThat(ex.getCause(), sameInstance(e));
      return;
    }
    fail("Should throw exception");
  }
示例#3
0
  @Test
  public void runAThrowingRunnableUnchecked() {
    OneTimeToggle toggled = new OneTimeToggle();
    DiggExceptions.runUnchecked(
        () ->
            toggled.nowOrIfAlreadyThenThrow(() -> new AssertionError("should not be run twice!")));
    assertTrue(toggled.yet());

    Exception e = new Exception();
    try {
      runUnchecked(
          () -> {
            throw e;
          });
    } catch (RuntimeException ex) {
      assertThat(ex.getCause(), sameInstance(e));
      return;
    }
    fail("Should throw exception");
  }