com.google.common.truth.ThrowableSubject.hasCauseThat()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.4k)|赞(0)|评价(0)|浏览(72)

本文整理了Java中com.google.common.truth.ThrowableSubject.hasCauseThat()方法的一些代码示例,展示了ThrowableSubject.hasCauseThat()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ThrowableSubject.hasCauseThat()方法的具体详情如下:
包路径:com.google.common.truth.ThrowableSubject
类名称:ThrowableSubject
方法名:hasCauseThat

ThrowableSubject.hasCauseThat介绍

[英]Returns a new ThrowableSubject that supports assertions on this throwable's direct cause. This method can be invoked repeatedly (e.g. assertThat(e).hasCauseThat().hasCauseThat().... to assert on a particular indirect cause.
[中]返回一个新的ThrowableSubject,该主题支持对该throwable的直接原因的断言。此方法可以重复调用(例如assertThat(e))。hasCauseThat()。hasCauseThat()。。。。断言某一特定的间接原因。

代码示例

代码示例来源:origin: google/guava

public void testGetRootCause_Loop() {
 Exception cause = new Exception();
 Exception exception = new Exception(cause);
 cause.initCause(exception);
 try {
  Throwables.getRootCause(cause);
  fail("Should have throw IAE");
 } catch (IllegalArgumentException expected) {
  assertThat(expected).hasCauseThat().isSameAs(cause);
 }
}

代码示例来源:origin: google/guava

public void testGetCasualChainLoop() {
 Exception cause = new Exception();
 Exception exception = new Exception(cause);
 cause.initCause(exception);
 try {
  Throwables.getCausalChain(cause);
  fail("Should have throw IAE");
 } catch (IllegalArgumentException expected) {
  assertThat(expected).hasCauseThat().isSameAs(cause);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedUntimed_ExecutionExceptionChecked() {
 try {
  getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, TwoArgConstructorException.class);
  fail();
 } catch (TwoArgConstructorException expected) {
  assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedTimed_withGoodAndBadExceptionConstructor() {
 try {
  getChecked(
    FAILED_FUTURE_CHECKED_EXCEPTION,
    ExceptionWithGoodAndBadConstructor.class,
    1,
    TimeUnit.SECONDS);
  fail();
 } catch (ExceptionWithGoodAndBadConstructor expected) {
  assertThat(expected).hasCauseThat().isSameAs(CHECKED_EXCEPTION);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedTimed_ExecutionExceptionChecked() {
 try {
  getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, TwoArgConstructorException.class, 0, SECONDS);
  fail();
 } catch (TwoArgConstructorException expected) {
  assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedUntimed_ExecutionExceptionUnchecked()
  throws TwoArgConstructorException {
 try {
  getChecked(FAILED_FUTURE_UNCHECKED_EXCEPTION, TwoArgConstructorException.class);
  fail();
 } catch (UncheckedExecutionException expected) {
  assertThat(expected).hasCauseThat().isEqualTo(UNCHECKED_EXCEPTION);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedUntimed_ExecutionExceptionError() throws TwoArgConstructorException {
 try {
  getChecked(FAILED_FUTURE_ERROR, TwoArgConstructorException.class);
  fail();
 } catch (ExecutionError expected) {
  assertThat(expected).hasCauseThat().isEqualTo(ERROR);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedTimed_ExecutionExceptionOtherThrowable() {
 try {
  getChecked(FAILED_FUTURE_OTHER_THROWABLE, TwoArgConstructorException.class, 0, SECONDS);
  fail();
 } catch (TwoArgConstructorException expected) {
  assertThat(expected).hasCauseThat().isEqualTo(OTHER_THROWABLE);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedUntimed_ExecutionExceptionOtherThrowable() {
 try {
  getChecked(FAILED_FUTURE_OTHER_THROWABLE, TwoArgConstructorException.class);
  fail();
 } catch (TwoArgConstructorException expected) {
  assertThat(expected).hasCauseThat().isEqualTo(OTHER_THROWABLE);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedUntimed_withGoodAndBadExceptionConstructor() throws Exception {
 try {
  getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, ExceptionWithGoodAndBadConstructor.class);
  fail();
 } catch (ExceptionWithGoodAndBadConstructor expected) {
  assertThat(expected).hasCauseThat().isSameAs(CHECKED_EXCEPTION);
 }
}

代码示例来源:origin: google/guava

public void testFailed() {
 Exception failureCause = new Exception();
 try {
  getDone(immediateFailedFuture(failureCause));
  fail();
 } catch (ExecutionException expected) {
  assertThat(expected).hasCauseThat().isEqualTo(failureCause);
 }
}

代码示例来源:origin: google/guava

public void testGetCheckedUntimed_exceptionClassUsedInitCause() {
 try {
  getChecked(FAILED_FUTURE_CHECKED_EXCEPTION, ExceptionWithoutThrowableConstructor.class);
  fail();
 } catch (ExceptionWithoutThrowableConstructor expected) {
  assertThat(expected).hasMessageThat().contains("mymessage");
  assertThat(expected).hasCauseThat().isEqualTo(CHECKED_EXCEPTION);
 }
}

代码示例来源:origin: google/guava

public void testNewDataInput_readFullyAndThenSome() {
 ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
 byte[] actual = new byte[bytes.length * 2];
 try {
  in.readFully(actual);
  fail("expected exception");
 } catch (IllegalStateException ex) {
  assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class);
 }
}

代码示例来源:origin: google/guava

/**
 * This test reproduces the bug in canonicalizeWildcardType() when the type variable is
 * recursively bounded.
 */
public void testRecursiveWildcardSubtypeBug() throws Exception {
 try {
  new RecursiveTypeBoundBugExample<>().testAllDeclarations();
  fail();
 } catch (Exception e) {
  assertThat(e).hasCauseThat().isInstanceOf(AssertionError.class);
 }
}

代码示例来源:origin: google/guava

public void testInvokeSubscriberMethod_exceptionWrapping() throws Throwable {
 Method method = getTestSubscriberMethod("exceptionThrowingMethod");
 Subscriber subscriber = Subscriber.create(bus, this, method);
 try {
  subscriber.invokeSubscriberMethod(FIXTURE_ARGUMENT);
  fail("Subscribers whose methods throw must throw InvocationTargetException");
 } catch (InvocationTargetException expected) {
  assertThat(expected).hasCauseThat().isInstanceOf(IntentionalException.class);
 }
}

代码示例来源:origin: google/guava

public void testNewDataInput_readByte() {
 ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
 for (int i = 0; i < bytes.length; i++) {
  assertEquals(bytes[i], in.readByte());
 }
 try {
  in.readByte();
  fail("expected exception");
 } catch (IllegalStateException ex) {
  assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class);
 }
}

代码示例来源:origin: google/guava

public void testNewDataInput_readUnsignedByte() {
 ByteArrayDataInput in = ByteStreams.newDataInput(bytes);
 for (int i = 0; i < bytes.length; i++) {
  assertEquals(bytes[i], in.readUnsignedByte());
 }
 try {
  in.readUnsignedByte();
  fail("expected exception");
 } catch (IllegalStateException ex) {
  assertThat(ex).hasCauseThat().isInstanceOf(EOFException.class);
 }
}

代码示例来源:origin: google/guava

@Override
public void testNulls() throws Exception {
 try {
  super.testNulls();
 } catch (AssertionFailedError e) {
  assertWithMessage("Method did not throw null pointer OR element not in graph exception.")
    .that(e)
    .hasCauseThat()
    .hasMessageThat()
    .contains(ERROR_ELEMENT_NOT_IN_GRAPH);
 }
}

代码示例来源:origin: google/guava

public void testFailingServiceStartAndWait() throws Exception {
 StartFailingService service = new StartFailingService();
 RecordingListener listener = RecordingListener.record(service);
 try {
  service.startAsync().awaitRunning();
  fail();
 } catch (IllegalStateException e) {
  assertEquals(EXCEPTION, service.failureCause());
  assertThat(e).hasCauseThat().isEqualTo(EXCEPTION);
 }
 assertEquals(ImmutableList.of(State.STARTING, State.FAILED), listener.getStateHistory());
}

代码示例来源:origin: google/guava

public void testThrowingServiceStartAndWait() throws Exception {
 StartThrowingService service = new StartThrowingService();
 RecordingListener listener = RecordingListener.record(service);
 try {
  service.startAsync().awaitRunning();
  fail();
 } catch (IllegalStateException e) {
  assertEquals(service.exception, service.failureCause());
  assertThat(e).hasCauseThat().isEqualTo(service.exception);
 }
 assertEquals(ImmutableList.of(State.STARTING, State.FAILED), listener.getStateHistory());
}

相关文章