本文整理了Java中org.assertj.core.api.Assertions.catchThrowable()
方法的一些代码示例,展示了Assertions.catchThrowable()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assertions.catchThrowable()
方法的具体详情如下:
包路径:org.assertj.core.api.Assertions
类名称:Assertions
方法名:catchThrowable
[英]Allows catching a Throwable more easily when used with Java 8 lambdas.
This caught Throwable can then be asserted.
Example:
@Test
public void testException() {
// when
Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
// then
assertThat(thrown).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
[中]当与Java8Lambdas一起使用时,可以更轻松地捕获一次性文件。
然后就可以断言这种捕获的可丢弃性。
例子:
@Test
public void testException() {
// when
Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
// then
assertThat(thrown).isInstanceOf(Exception.class)
.hasMessageContaining("boom");
}
代码示例来源:origin: azkaban/azkaban
@Test
public void can_not_submit_tasks_after_shutdown() throws
InterruptedException {
// given
final ExecutorService service = Executors.newSingleThreadExecutor();
service.submit(this::sleep);
// when
this.executorServiceUtils.gracefulShutdown(service, Duration.ofMillis(1));
final Throwable thrown = catchThrowable(() -> service.submit(this::sleep));
// then
assertThat(thrown).isInstanceOf(RejectedExecutionException.class);
}
代码示例来源:origin: azkaban/azkaban
private void assertGetUploadedFileOfCleanedVersion(final int project, final int version) {
final Throwable thrown = catchThrowable(() -> this.loader.getUploadedFile(project, version));
assertThat(thrown).isInstanceOf(ProjectManagerException.class);
assertThat(thrown).hasMessageStartingWith(String.format("Got numChunks=0 for version %s of "
+ "project %s - seems like this version has been cleaned up", version, project));
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void login_cant_be_empty() {
Throwable thrown = catchThrowable(() -> new Credentials("", "bar"));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("login must not be null nor empty");
thrown = catchThrowable(() -> new Credentials(null, "bar"));
assertThat(thrown)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("login must not be null nor empty");
Credentials underTest = new Credentials("foo", "bar");
assertThat(underTest.getLogin()).isEqualTo("foo");
}
代码示例来源:origin: azkaban/azkaban
@Test
public void testLocalExecutorScenario() {
this.props.put(ConfigurationKeys.EXECUTOR_PORT, 12345);
final Throwable thrown = catchThrowable(() -> createExecutorManager());
assertThat(thrown).isInstanceOf(IllegalArgumentException.class);
assertThat(thrown.getMessage()).isEqualTo(
"azkaban.use.multiple.executors must be true. Single executor mode is not supported any more.");
}
代码示例来源:origin: apache/geode
@Test
public void createAlertListenerMessage_requiresInternalDistributedMember() {
member = mock(DistributedMember.class);
Throwable thrown = catchThrowable(
() -> alertListenerMessageFactory.createAlertListenerMessage(member, AlertLevel.WARNING,
new Date(), "connectionName", "threadName", "formattedMessage", null));
assertThat(thrown).isNotNull().isInstanceOf(IllegalArgumentException.class);
}
}
代码示例来源:origin: line/armeria
@Test
public void execute_reachedMaxAttempts() throws Exception {
final HelloService.Iface client = helloClient(retryAlways, 2);
when(serviceHandler.hello(anyString())).thenThrow(new IllegalArgumentException());
final Throwable thrown = catchThrowable(() -> client.hello("hello"));
assertThat(thrown).isInstanceOf(TApplicationException.class);
assertThat(((TApplicationException) thrown).getType()).isEqualTo(TApplicationException.INTERNAL_ERROR);
verify(serviceHandler, times(2)).hello("hello");
}
代码示例来源:origin: apache/geode
@Test
public void fromJsonWithWhiteSpaceStringThrowsIllegalArgumentException() {
// given: white space string
String whiteSpaceString = " ";
// when: passed to fromJson
Throwable thrown = catchThrowable(() -> fromJson(whiteSpaceString));
// then: throws IllegalArgumentException with cause of GfJsonException
assertThat(thrown).isInstanceOf(IllegalArgumentException.class)
.hasCauseInstanceOf(GfJsonException.class);
assertThat(thrown.getCause()).isInstanceOf(GfJsonException.class).hasNoCause();
}
代码示例来源:origin: line/armeria
@Test
public void testFailedCompletedFuture() throws Exception {
final ThriftCompletableFuture<String> future = failedCompletedFuture(new IllegalStateException());
assertThat(catchThrowable(future::get)).hasCauseInstanceOf(IllegalStateException.class);
}
代码示例来源:origin: apache/geode
@Test
public void hasCauseTypeOfNullClassShouldThrowNullPointerException() {
Throwable thrown = catchThrowable(() -> hasCauseType(new Exception(), null));
assertThat(thrown).isExactlyInstanceOf(NullPointerException.class);
}
代码示例来源:origin: line/armeria
@Test
public void propagateLastResponseWhenNextRetryIsAfterTimeout() throws Exception {
final RetryStrategyWithContent<RpcResponse> strategy =
(ctx, response) -> CompletableFuture.completedFuture(Backoff.fixed(10000000));
final HelloService.Iface client = helloClient(strategy, 100);
when(serviceHandler.hello(anyString())).thenThrow(new IllegalArgumentException());
final Throwable thrown = catchThrowable(() -> client.hello("hello"));
assertThat(thrown).isInstanceOf(TApplicationException.class);
assertThat(((TApplicationException) thrown).getType()).isEqualTo(TApplicationException.INTERNAL_ERROR);
verify(serviceHandler, only()).hello("hello");
}
代码示例来源:origin: line/armeria
@Test
public void build_wrongScheme() throws Exception {
final Throwable thrown = catchThrowable(
() -> new ArmeriaRetrofitBuilder().baseUrl("foo://example.com:8080").build());
assertThat(thrown).isInstanceOf(IllegalArgumentException.class)
.hasMessage("baseUrl must have an HTTP scheme: foo://example.com:8080");
}
代码示例来源:origin: line/armeria
@Test
public void build_withNonRootPathNonSlashEnd() throws Exception {
final Throwable thrown = catchThrowable(
() -> new ArmeriaRetrofitBuilder().baseUrl("http://example.com:8080/a/b/c").build());
assertThat(thrown).isInstanceOf(IllegalArgumentException.class)
.hasMessage("baseUrl must end with /: http://example.com:8080/a/b/c");
}
代码示例来源:origin: spring-projects/spring-security
@Test
public void loadConfigWhenWebSecurityConfigurersHaveSameOrderThenThrowBeanCreationException() throws Exception {
Throwable thrown = catchThrowable(() -> this.spring.register(DuplicateOrderConfig.class).autowire());
assertThat(thrown).isInstanceOf(BeanCreationException.class)
.hasMessageContaining("@Order on WebSecurityConfigurers must be unique")
.hasMessageContaining(DuplicateOrderConfig.WebConfigurer1.class.getName())
.hasMessageContaining(DuplicateOrderConfig.WebConfigurer2.class.getName());
}
代码示例来源:origin: apache/geode
@Test
public void overMaxAllocationFails() {
setUpSingleSlabManager();
OutOfOffHeapMemoryListener ooohml = mock(OutOfOffHeapMemoryListener.class);
when(this.ma.getOutOfOffHeapMemoryListener()).thenReturn(ooohml);
Throwable thrown = catchThrowable(() -> this.freeListManager.allocate(DEFAULT_SLAB_SIZE - 7));
verify(ooohml).outOfOffHeapMemory((OutOfOffHeapMemoryException) thrown);
}
代码示例来源:origin: apache/geode
@Test
public void hasCauseMessageForNullShouldThrowNullPointerException() {
Throwable thrown = catchThrowable(() -> hasCauseMessage(null, "message"));
assertThat(thrown).isExactlyInstanceOf(NullPointerException.class);
}
代码示例来源:origin: line/armeria
@Test
public void testFailedListenableFuture() throws Exception {
assumeUnshadedGuava();
final ThriftListenableFuture<String> future = failedListenableFuture(new IllegalStateException());
assertThat(catchThrowable(future::get)).hasCauseInstanceOf(IllegalStateException.class);
}
代码示例来源:origin: line/armeria
@Test
public void tooLargeRequest_uncompressed() throws Exception {
final SimpleRequest request = newLargeRequest();
final StatusRuntimeException t =
(StatusRuntimeException) catchThrowable(
() -> blockingClient.staticUnaryCall(request));
assertThat(t.getStatus().getCode()).isEqualTo(Code.CANCELLED);
checkRequestLogStatus(grpcStatus -> {
assertThat(grpcStatus.getCode()).isEqualTo(Code.RESOURCE_EXHAUSTED);
});
}
代码示例来源:origin: apache/geode
@Test
public void hasCauseMessageForNullMessageShouldThrowNullPointerException() {
Throwable thrown = catchThrowable(() -> hasCauseMessage(new OneException((String) null), null));
assertThat(thrown).isExactlyInstanceOf(NullPointerException.class);
}
代码示例来源:origin: line/armeria
@Test
public void tooLargeRequest_compressed() throws Exception {
final SimpleRequest request = newLargeRequest();
final StatusRuntimeException t =
(StatusRuntimeException) catchThrowable(
() -> blockingClient.withCompression("gzip").staticUnaryCall(request));
assertThat(t.getStatus().getCode()).isEqualTo(Code.CANCELLED);
checkRequestLogStatus(grpcStatus -> {
assertThat(grpcStatus.getCode()).isEqualTo(Code.RESOURCE_EXHAUSTED);
});
}
代码示例来源:origin: SonarSource/sonarqube
@Test
public void fail_throws_exception_if_task_is_pending() {
CeTask task = submit(CeTaskTypes.REPORT, newComponent(randomAlphabetic(12)));
CeQueueDto queueDto = db.getDbClient().ceQueueDao().selectByUuid(db.getSession(), task.getUuid()).get();
Throwable thrown = catchThrowable(() -> underTest.fail(db.getSession(), queueDto, "TIMEOUT", "Failed on timeout"));
assertThat(thrown)
.isInstanceOf(IllegalStateException.class)
.hasMessage("Task is not in-progress and can't be marked as failed [uuid=" + task.getUuid() + "]");
}
内容来源于网络,如有侵权,请联系作者删除!