ASSERTJ:使用方法sostthatby时如何返回实际异常
我正在从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ASSERTJ提供两种用于检查自定义例外的样式:
catchthrowableTyoftype
assertthatexceptionoftype
保持示例的样式,可以使用
CATCHTHROWABLE
:或,使用
catchthrowableabyoftype
:AssertJ offers two styles for checking custom exceptions:
catchThrowableOfType
assertThatExceptionOfType
Keeping the style of your example, you can write the following with
catchThrowable
:or, with
catchThrowableOfType
:Assertj的方式如下:
The AssertJ way is following: