org.assertj.core.api.Assertions.assertThatThrownBy()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(356)

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

Assertions.assertThatThrownBy介绍

[英]Allows to capture and then assert on a Throwable (easier done with lambdas).

Java 8 example :

@Test 
public void testException() { 
assertThatThrownBy(() -> { throw new Exception("boom!"); }).isInstanceOf(Exception.class) 
.hasMessageContaining("boom"); 
}

If the provided ThrowingCallable does not raise an exception, an error is immediately thrown, in that case the test description provided with AbstractAssert#as(String,Object...) is not honored.
To use a test description, use #catchThrowable(ThrowableAssert.ThrowingCallable) as shown below:

// assertion will fail but "display me" won't appear in the error 
assertThatThrownBy(() -> {}).as("display me") 
.isInstanceOf(Exception.class); 
// assertion will fail AND "display me" will appear in the error 
Throwable thrown = catchThrowable(() -> {}); 
assertThat(thrown).as("display me") 
.isInstanceOf(Exception.class);

Alternatively you can also use assertThatCode(ThrowingCallable) for the test description provided with AbstractAssert#as(String,Object...) to always be honored.
[中]允许捕获并在可丢弃文件上断言(使用lambdas更容易实现)。
Java 8示例:

@Test 
public void testException() { 
assertThatThrownBy(() -> { throw new Exception("boom!"); }).isInstanceOf(Exception.class) 
.hasMessageContaining("boom"); 
}

如果提供的ThrowingCallable没有引发异常,则会立即抛出错误,在这种情况下,将使用AbstractAssert#as(字符串、对象…)提供的测试描述这是不光荣的。
要使用测试描述,请使用#catchThrowable(ThrowableAssert.ThrowingCallable),如下所示:

// assertion will fail but "display me" won't appear in the error 
assertThatThrownBy(() -> {}).as("display me") 
.isInstanceOf(Exception.class); 
// assertion will fail AND "display me" will appear in the error 
Throwable thrown = catchThrowable(() -> {}); 
assertThat(thrown).as("display me") 
.isInstanceOf(Exception.class);

或者,您也可以使用assertThatCode(ThrowingCallable)作为AbstractAssert#as(字符串、对象…)提供的测试描述永远被尊重。

代码示例

代码示例来源:origin: prestodb/presto

@Test
public void testDataFormatValidation()
{
  for (Type type : asList(TIMESTAMP, DOUBLE)) {
    assertThatThrownBy(() -> singleColumnDecoder(type, "wrong_format"))
        .isInstanceOf(PrestoException.class)
        .hasMessage("unknown data format 'wrong_format' used for column 'some_column'");
  }
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void constructorWhenAuthorizedClientServiceIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(null))
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void setAuthorizedClientRepositoryWhenAuthorizedClientRepositoryIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> this.authorizedClientRepository.setAnonymousAuthorizedClientRepository(null))
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

private void assertWrongDataFormatException(ThrowableAssert.ThrowingCallable callable)
{
  assertThatThrownBy(callable)
      .isInstanceOf(PrestoException.class)
      .hasMessageMatching("Wrong dataFormat .* specified for column .*");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void saveAuthorizedClientWhenAuthorizedClientIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> this.authorizedClientRepository.saveAuthorizedClient(null, null, this.exchange).block())
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

private void assertUnsupportedColumnTypeException(ThrowableAssert.ThrowingCallable callable)
{
  assertThatThrownBy(callable)
      .isInstanceOf(PrestoException.class)
      .hasMessageMatching("Unsupported column type .* for column .*");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void setRequestEntityConverterWhenConverterIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> this.tokenResponseClient.setRequestEntityConverter(null))
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

@Test
public void testInvalidConstructorArguments()
{
  assertThatThrownBy(() -> new OperationTimer(false, true)).isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void setRestOperationsWhenRestOperationsIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> this.tokenResponseClient.setRestOperations(null))
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

private void assertUnsupportedColumnTypeException(ThrowingCallable callable)
{
  assertThatThrownBy(callable)
      .isInstanceOf(PrestoException.class)
      .hasMessageMatching("unsupported column type .* for column .*");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void removeAuthorizedClientWhenRequestIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> this.authorizedClientRepository.removeAuthorizedClient(
      this.registrationId1, null, null, this.response)).isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

private void assertRuntimeDecodingFailure(ThrowableAssert.ThrowingCallable callable)
  {
    assertThatThrownBy(callable)
        .isInstanceOf(PrestoException.class)
        .hasMessageMatching("could not parse value .* as .* for column .*");
  }
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void setErrorConverterWhenConverterIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> this.messageConverter.setErrorConverter(null))
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

private void assertUnsupportedColumnTypeException(ThrowableAssert.ThrowingCallable callable)
{
  assertThatThrownBy(callable)
      .isInstanceOf(PrestoException.class)
      .hasMessageMatching("Unsupported column type .* for column .*");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void constructorWhenAuthorizedClientServiceIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(null))
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

private void assertMappingDoesNotMatchDataFormatException(ThrowableAssert.ThrowingCallable callable)
{
  assertThatThrownBy(callable)
      .isInstanceOf(PrestoException.class)
      .hasMessageContaining("Bytes mapping for column 'some_column' does not match dataFormat");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void constructorTokenRequestResponseWhenAuthorizationExchangeIsNullThenThrowIllegalArgumentException() {
  assertThatThrownBy(() -> new OAuth2AuthorizationCodeAuthenticationToken(this.clientRegistration, null, this.accessToken))
      .isInstanceOf(IllegalArgumentException.class);
}

代码示例来源:origin: prestodb/presto

private void assertUnsupportedColumnTypeException(ThrowableAssert.ThrowingCallable callable)
{
  assertThatThrownBy(callable)
      .isInstanceOf(PrestoException.class)
      .hasMessageMatching("Unsupported column type .* for column .*");
}

代码示例来源:origin: spring-projects/spring-security

@Test
public void matchesWhenUnMappedThenIllegalArgumentException() {
  assertThatThrownBy(() -> this.passwordEncoder.matches(this.rawPassword, "{unmapped}" + this.rawPassword))
    .isInstanceOf(IllegalArgumentException.class)
    .hasMessage("There is no PasswordEncoder mapped for the id \"unmapped\"");
  verifyZeroInteractions(this.bcrypt, this.noop);
}

代码示例来源:origin: prestodb/presto

@Test
public void testInvalidDataFormat()
{
  assertThatThrownBy(() -> singleColumnDecoder(BigintType.BIGINT, "0", "format", null, false, false, false))
      .isInstanceOf(PrestoException.class)
      .hasMessageMatching("invalid dataFormat 'format' for column 'some_column'");
}

相关文章