ASSERTJ:使用方法sostthatby时如何返回实际异常

发布于 2025-02-12 02:54:52 字数 1269 浏览 1 评论 0原文

我正在从Junit 5和Hamcrest主张迁移到Assertj,我无法找到正确从可执行文件中提取实际例外的方法。这是一个Junit/Hamcrest示例:

var result = assertThrows(MyException.class, () -> this.objectUnderTest.someMethod(param1, param2, param3));
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

我想拥有的是SMTH。如(assertj),

var result = assertThatThrownBy(() -> this.objectUnderTest.someMethod(param1, param2, param3))
 .isInstanceOf(MyException.class);
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

但是从ASSERTJ版本开始3.21.0 assertthatbyby方法为我提供了Abstract throverthrowableassert&lt<?,?的实例?扩展可投掷>类,我找不到任何可以给我的方法,然后myException实例。因此,目前我最终使用了另一种方法,然后手动投射到myException

Throwable throwable = Assertions.catchThrowable(() -> this.objectUnderTest.doFilterInternal(param1, param2, param3));
MyException exception = (MyException) throwable;
assertThat(exception.getError().getErrorCode(), equalTo(someValue));
assertThat(exception.getError().getDescription(), equalTo("someString"));

I am migrating from JUnit 5 and Hamcrest Assertions to AssertJ and I can't figure out the right method for extracting the actual exception from the executable. Here is a JUnit/Hamcrest example:

var result = assertThrows(MyException.class, () -> this.objectUnderTest.someMethod(param1, param2, param3));
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

What I would like to have is smth. like (AssertJ)

var result = assertThatThrownBy(() -> this.objectUnderTest.someMethod(param1, param2, param3))
 .isInstanceOf(MyException.class);
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

But as of the AssertJ version 3.21.0 the assertThatThrownBy method gives me an instance of AbstractThrowableAssert<?, ? extends Throwable> class and I can't find any method which would give me then the MyException instance. So for now I ended up using another method and casting manually to MyException:

Throwable throwable = Assertions.catchThrowable(() -> this.objectUnderTest.doFilterInternal(param1, param2, param3));
MyException exception = (MyException) throwable;
assertThat(exception.getError().getErrorCode(), equalTo(someValue));
assertThat(exception.getError().getDescription(), equalTo("someString"));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静待花开 2025-02-19 02:54:52

ASSERTJ提供两种用于检查自定义例外的样式:

保持示例的样式,可以使用CATCHTHROWABLE

Throwable throwable = catchThrowable(() -> this.objectUnderTest.someMethod(param1, param2, param3));

assertThat(throwable)
  .asInstanceOf(InstanceOfAssertFactories.throwable(MyException.class))
  .extracting(MyException::getError)
  .returns(1, from(Error::getErrorCode))
  .returns("something", from(Error::getDescription));

或,使用catchthrowableabyoftype

MyException exception = catchThrowableOfType(() -> this.objectUnderTest.someMethod(param1, param2, param3), MyException.class);

assertThat(exception)
  .extracting(MyException::getError)
  .returns(1, from(Error::getErrorCode))
  .returns("something", from(Error::getDescription));

AssertJ offers two styles for checking custom exceptions:

Keeping the style of your example, you can write the following with catchThrowable:

Throwable throwable = catchThrowable(() -> this.objectUnderTest.someMethod(param1, param2, param3));

assertThat(throwable)
  .asInstanceOf(InstanceOfAssertFactories.throwable(MyException.class))
  .extracting(MyException::getError)
  .returns(1, from(Error::getErrorCode))
  .returns("something", from(Error::getDescription));

or, with catchThrowableOfType:

MyException exception = catchThrowableOfType(() -> this.objectUnderTest.someMethod(param1, param2, param3), MyException.class);

assertThat(exception)
  .extracting(MyException::getError)
  .returns(1, from(Error::getErrorCode))
  .returns("something", from(Error::getDescription));
预谋 2025-02-19 02:54:52
  var result = assertthatthrownby((() - &gt;
this.objectundertest.somemethod(param1,param2,param3)) 
.isinstanceOf(myException.class);
assertthat(result.getError()。getErrorCode(),均等(someValue));
assertthat(result.getError()。getDescription(),等于(“ somestring”));
 

Assertj的方式如下:

assertThatExceptionOfType(MyException.class)
        .isThrownBy(() -> this.objectUnderTest.someMethod(param1, param2, param3))
        .satisfies(ex -> assertThat(ex.getError().getErrorCode()).isEqualTo(someValue));
var result = assertThatThrownBy(() ->
this.objectUnderTest.someMethod(param1, param2, param3)) 
.isInstanceOf(MyException.class);
assertThat(result.getError().getErrorCode(), equalTo(someValue));
assertThat(result.getError().getDescription(), equalTo("someString"));

The AssertJ way is following:

assertThatExceptionOfType(MyException.class)
        .isThrownBy(() -> this.objectUnderTest.someMethod(param1, param2, param3))
        .satisfies(ex -> assertThat(ex.getError().getErrorCode()).isEqualTo(someValue));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文