您可以将自定义消息添加到AssertJ assertThat吗? [英] Can you add a custom message to AssertJ assertThat?

查看:133
本文介绍了您可以将自定义消息添加到AssertJ assertThat吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个测试套件,主要使用带有Hamcrest匹配器的JUnit断言.我们的一个团队开始对 AssertJ 进行试验,并以其语法,灵活性和声明性给人留下了深刻的印象. JUnit提供的一项功能是我无法在AssertJ中找到与之等效的功能:添加自定义断言失败消息.

We have a test suite that primarily uses JUnit assertions with Hamcrest matchers. One of our team started experimenting with AssertJ and impressed people with its syntax, flexibility and declarative-ness. There is one feature that JUnit provides that I can't find an equivalent for in AssertJ: adding a custom assert failure message.

我们经常在比较那些不是为了人类可读性而制成的对象,这些对象将具有随机寻找的Id或UUID,并且无法通过其中包含的数据来判断它们应该是什么.不幸的是,对于我们的代码库来说,这是不可避免的情况,因为它实现的目的之一就是在其他服务之间映射数据而不必了解它是什么.

We're often comparing objects that are not made for human readability and will have random-seeming Ids or UUIDs and it's impossible to tell what they're supposed to be by the data they contain. This is an unavoidable situation for our codebase, sadly, as part of the purpose it fulfills is mapping data between other services without necessarily understanding what it is.

在JUnit中, assertThat 方法在Matcher<T>参数之前提供了带有String reason参数的版本.这样一来,添加一个简短的调试字符串就可以轻而易举地解决问题,就像进行比较对人类意味着什么一样.

In JUnit, the assertThat method provides a version with a String reason parameter before the Matcher<T> param. This makes it trivial to add a short debug string shedding some light on the problem, like what the comparison should mean to a human.

AssertJ提供了数十万种不同的接口Assert 或其众多实现类之一.该界面未提供设置失败时包含的自定义消息的标准方法.

AssertJ, on the other hand, provides a jillion different genericized static assertThat methods which return some form of interface Assert or one of its many implementing classes. This interface does not provide a standard way of setting a custom message to be included with failures.

是否有任何方法可以从AssertJ API或其扩展之一获得此功能,而不必

Is there any way to get this functionality from the AssertJ API or one of its extensions without having to create a custom assert class for every assert type we want to add messages to?

推荐答案

以经典方式,发布问题后,我发现了自己想要的片刻.希望这将使下一个人更容易找到,而不必首先知道它的名字.神奇的方法是名称上被骗人的

And in classic fashion, I found what I was looking for moments after posting the question. Hopefully this will make it easier for the next person to find without first having to know what it's called. The magic method is the deceptively short-named as, which is part of another interface that AbstractAssert implements: Descriptable, not the base Assert interface.

public S as(String description, Object... args)

设置支持String.format(String, Object...)语法的对象的说明.
例子:

Sets the description of this object supporting String.format(String, Object...) syntax.
Example :

try {
  // set a bad age to Mr Frodo which is really 33 years old.
  frodo.setAge(50);
  // you can specify a test description with as() method or describedAs(), it supports String format args
  assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33);
} catch (AssertionError e) {
  assertThat(e).hasMessage("[check Frodo's age] expected:<[33]> but was:<[50]>");
}

如果断言失败,则捕获在模块hasMessage中的带引号的字符串将出现在单元测试输出日志中.

Where that quoted string in the catch block hasMessage is what appears in your unit test output log if the assertion fails.

我通过注意到问题中链接的自定义断言页面. JavaDoc 指出它已受到保护,因此调用者无法使用它来设置自定义消息.但是,它确实提到了as助手:

I found this by noticing the failWithMessage helper in the custom assert page linked in the question. The JavaDoc for that method points out that it is protected, so it can't be used by callers to set a custom message. It does however mention the as helper:

此外,此方法支持使用as(String, Object...)设置的任何描述或用户使用overridingErrorMessage(String, Object...)定义的覆盖错误消息.

Moreover, this method honors any description set with as(String, Object...) or overridden error message defined by the user with overridingErrorMessage(String, Object...).

...和

... and the overridingErrorMessage helper, which completely replaces the standard AssertJ expected: ... but was:... message with the new string provided.

在功能突出显示页面之前,AssertJ主页没有提到任何一个助手,该页面在

The AssertJ homepage doesn't mention either helper until the features highlights page, which shows examples of the as helper in the Soft Assertions section, but doesn't directly describe what it does.

这篇关于您可以将自定义消息添加到AssertJ assertThat吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆