Java Code Examples for org.assertj.core.api.Assertions#catchThrowable()

The following examples show how to use org.assertj.core.api.Assertions#catchThrowable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReactorTest.java    From BlockHound with Apache License 2.0 6 votes vote down vote up
@Test
public void negative() throws Exception {
    var mono = publisher.call().hide();
    var e = Assertions.catchThrowable(() -> {
        mono.subscribeOn(Schedulers.parallel()).block(Duration.ofSeconds(1));
    });
    assertThat(e).hasCauseInstanceOf(BlockingOperationError.class);

    e = e.getCause();
    e.printStackTrace(System.out);

    assertThat(e).isInstanceOfSatisfying(BlockingOperationError.class, it -> {
        assertThat(it.getMethod())
                .isNotNull()
                .returns(method, BlockingMethod::toString);
    });
}
 
Example 2
Source File: ReactorTest.java    From BlockHound with Apache License 2.0 6 votes vote down vote up
@Test
public void negativeWithFlux() throws Exception {
    var mono = Flux.from(publisher.call()).hide();
    var e = Assertions.catchThrowable(() -> {
        mono.subscribeOn(Schedulers.parallel()).blockLast(Duration.ofSeconds(1));
    });
    assertThat(e).hasCauseInstanceOf(BlockingOperationError.class);

    e = e.getCause();
    e.printStackTrace(System.out);

    assertThat(e).isInstanceOfSatisfying(BlockingOperationError.class, it -> {
        assertThat(it.getMethod())
                .isNotNull()
                .returns(method, BlockingMethod::toString);
    });
}
 
Example 3
Source File: CustomBlockingMethodTest.java    From BlockHound with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReportCustomBlockingMethods() {
    Throwable e = Assertions.catchThrowable(() -> {
        Mono.fromRunnable(Blocking::block).hide().subscribeOn(Schedulers.parallel()).block(Duration.ofMillis(100));
    });
    assertThat(e)
            .as("exception")
            .hasCauseInstanceOf(BlockingOperationError.class);

    assertThat(e.getCause()).isInstanceOfSatisfying(BlockingOperationError.class, cause -> {
        assertThat(cause.getMethod())
                .isNotNull()
                .returns(Blocking.class.getName(), BlockingMethod::getClassName)
                .returns("block", BlockingMethod::getName);
    });
}
 
Example 4
Source File: RoutingHandlerTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void doChannelRead_HttpRequest_throws_exception_when_content_length_header_greater_than_configured_global_request_limit() {
    // given
    doReturn(null).when(endpointMock).maxRequestSizeInBytesOverride();

    maxRequestSizeInBytes = 10;
    httpHeaders.set(CONTENT_LENGTH, 100);
    handlerSpy = spy(new RoutingHandler(endpoints, maxRequestSizeInBytes, distributedTracingConfigMock));

    // when
    Throwable thrownException = Assertions.catchThrowable(() -> handlerSpy.doChannelRead(ctxMock, msg));

    // then
    assertThat(thrownException).isExactlyInstanceOf(RequestTooBigException.class);
    assertThat(thrownException.getMessage()).isEqualTo("Content-Length header value exceeded configured max request size of 10");
}
 
Example 5
Source File: RoutingHandlerTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void doChannelRead_HttpRequest_throws_exception_when_failed_decoder_result() {
    // given
    HttpRequest msgMock = mock(HttpRequest.class);
    Throwable decoderFailureCauseMock = mock(Throwable.class);
    DecoderResult decoderResult = DecoderResult.failure(decoderFailureCauseMock);
    doReturn(decoderResult).when(msgMock).decoderResult();
    doReturn(null).when(stateMock).getRequestInfo();

    // when
    Throwable thrownException = Assertions.catchThrowable(() -> handlerSpy.doChannelRead(ctxMock, msgMock));

    // then
    assertThat(thrownException).isExactlyInstanceOf(InvalidHttpRequestException.class);
    assertThat(thrownException.getCause()).isSameAs(decoderFailureCauseMock);
}
 
Example 6
Source File: Jersey2WebApplicationExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null_projectApiErrors() {
    // when
    Throwable ex = Assertions.catchThrowable(new ThrowableAssert.ThrowingCallable() {
        @Override
        public void call() throws Throwable {
            new Jersey2WebApplicationExceptionHandlerListener(null, utils);
        }
    });

    // then
    assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 7
Source File: DownstreamNetworkExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null() {
    // when
    Throwable ex = Assertions.catchThrowable(new ThrowableAssert.ThrowingCallable() {
        @Override
        public void call() throws Throwable {
            new DownstreamNetworkExceptionHandlerListener(null);
        }
    });

    // then
    Assertions.assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 8
Source File: ServersideValidationErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null_projectApiErrors() {
    // when
    Throwable ex = Assertions.catchThrowable(new ThrowableAssert.ThrowingCallable() {
        @Override
        public void call() throws Throwable {
            new ServersideValidationErrorHandlerListener(null, ApiExceptionHandlerUtils.DEFAULT_IMPL);
        }
    });

    // then
    Assertions.assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 9
Source File: ClientDataValidationErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null_utils() {
    // when
    Throwable ex = Assertions.catchThrowable(new ThrowableAssert.ThrowingCallable() {
        @Override
        public void call() throws Throwable {
            new ClientDataValidationErrorHandlerListener(mock(ProjectApiErrors.class), null);
        }
    });

    // then
    Assertions.assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 10
Source File: Jersey1WebApplicationExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null_projectApiErrors() {
    // when
    Throwable ex = Assertions.catchThrowable(new ThrowableAssert.ThrowingCallable() {
        @Override
        public void call() throws Throwable {
            new Jersey1WebApplicationExceptionHandlerListener(null, utils);
        }
    });

    // then
    assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 11
Source File: RequestInfoSetterHandlerTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Test
public void doChannelRead_throws_exception_and_releases_content_when_no_request_info_when_HttpContent_message() {
    // given
    doReturn(null).when(stateMock).getRequestInfo();

    // when
    Throwable thrownException = Assertions.catchThrowable(() -> handler.doChannelRead(ctxMock, httpContentMock));

    // then
    assertThat(thrownException).isExactlyInstanceOf(IllegalStateException.class);
    verify(httpContentMock).release();
}
 
Example 12
Source File: OneOffSpringCommonFrameworkExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null_projectApiErrors() {
    // when
    Throwable ex = Assertions.catchThrowable(
        () -> new OneOffListenerBasicImpl(null, ApiExceptionHandlerUtils.DEFAULT_IMPL)
    );

    // then
    assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 13
Source File: JaxRsWebApplicationExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null_projectApiErrors() {
    // when
    Throwable ex = Assertions.catchThrowable(new ThrowableAssert.ThrowingCallable() {
        @Override
        public void call() throws Throwable {
            new JaxRsWebApplicationExceptionHandlerListener(null, utils);
        }
    });

    // then
    assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 14
Source File: S3UploadStepTest.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void fileOrIncludePathPatternMustBeDefined() throws Exception {
	S3UploadStep step = new S3UploadStep("my-bucket", false, false);
	S3UploadStep.Execution execution = new S3UploadStep.Execution(step, Mockito.mock(StepContext.class));
	Throwable t = Assertions.catchThrowable(execution::run);
	Assert.assertTrue(t instanceof IllegalArgumentException);
	Assert.assertEquals("File or IncludePathPattern must not be null", t.getMessage());
}
 
Example 15
Source File: OptionsValidatorTest.java    From swagger-brake with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateThrowsExceptionWhenMavenConfigIsSetExceptGroupId() {
    // given
    Options options = new Options();
    options.setNewApiPath("something");
    options.setMavenRepoUrl("localhost:8080/repo");
    options.setMavenSnapshotRepoUrl("localhost:8080/snapshot-repo");
    options.setCurrentArtifactVersion("1.0.0-SNAPSHOT");
    options.setArtifactId("swagger-brake");
    // when
    Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
    // then
    assertThat(result).isInstanceOf(IllegalArgumentException.class);
    assertThat(result.getMessage()).contains("groupId");
}
 
Example 16
Source File: OneOffSpringWebFluxFrameworkExceptionHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void constructor_throws_IllegalArgumentException_if_passed_null_utils() {
    // when
    Throwable ex = Assertions.catchThrowable(
        () -> new OneOffSpringWebFluxFrameworkExceptionHandlerListener(mock(ProjectApiErrors.class), null)
    );

    // then
    assertThat(ex).isInstanceOf(IllegalArgumentException.class);
}
 
Example 17
Source File: RequestInfoSetterHandlerTest.java    From riposte with Apache License 2.0 5 votes vote down vote up
@Test
public void doChannelRead_HttpContent_throws_exception_when_failed_decoder_result() {
    // given
    Throwable decoderFailureCauseMock = mock(Throwable.class);
    DecoderResult decoderResult = DecoderResult.failure(decoderFailureCauseMock);
    doReturn(decoderResult).when(httpContentMock).decoderResult();

    // when
    Throwable thrownException = Assertions.catchThrowable(() -> handler.doChannelRead(ctxMock, httpContentMock));

    // then
    assertThat(thrownException).isExactlyInstanceOf(InvalidHttpRequestException.class);
    assertThat(thrownException.getCause()).isSameAs(decoderFailureCauseMock);
    verify(httpContentMock).release();
}
 
Example 18
Source File: OptionsValidatorTest.java    From swagger-brake with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateThrowsExceptionWhenMavenRepoUrlIsSetButSnapshotRepoIsNot() {
    // given
    Options options = new Options();
    options.setNewApiPath("something");
    options.setMavenRepoUrl("localhost:8080/repo");
    options.setCurrentArtifactVersion("1.0.0-SNAPSHOT");
    options.setGroupId("io.redskap");
    options.setArtifactId("swagger-brake");
    // when
    Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
    // then
    assertThat(result).isInstanceOf(IllegalArgumentException.class);
    assertThat(result.getMessage()).contains("mavenSnapshotRepoUrl");
}
 
Example 19
Source File: OptionsValidatorTest.java    From swagger-brake with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateThrowsExceptionWhenNeitherOldApiPathNorMavenIsSet() {
    // given
    Options options = new Options();
    options.setNewApiPath("something");
    // when
    Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
    // then
    assertThat(result).isInstanceOf(IllegalArgumentException.class);
    assertThat(result.getMessage()).contains("oldApiPath");
    assertThat(result.getMessage()).containsIgnoringCase("maven");
}
 
Example 20
Source File: OptionsValidatorTest.java    From swagger-brake with Apache License 2.0 5 votes vote down vote up
@Test
public void testValidateThrowsExceptionWhenNewApiPathIsNotSet() {
    // given
    Options options = new Options();
    options.setOldApiPath("something");
    // when
    Throwable result = Assertions.catchThrowable(() -> underTest.validate(options));
    // then
    assertThat(result).isInstanceOf(IllegalArgumentException.class);
    assertThat(result.getMessage()).contains("newApiPath");
}