与 AssertJ 比较的真实断言库

Truth assertions library comparing to AssertJ

我用的是FEST-Assert,在它停止开发后搬到了AssertJ

最近我被指向 Google 存储库和另一个断言库 Truth (http://google.github.io/truth/).

阅读示例后,我发现开始使用它比 AssertJ 有任何优势。因此,使用什么只是品味问题。但也许我错过了重点,是吗?

来自 GitHub 的 comments 之一:

The core difference is that the design of Truth includes two specific areas of extensibility - that of a strategy for proposition failure - such that a "subject" for Integers, or a subject for Strings can be re-used in the context of completely different results for failure. A notable example is the distinction between JUnit's use of AssertionError and it's AssumptionViolationException. Truth lets you use the same proposition classes for both.

The other area of flexibility is the ability to create new assertion/proposition types and hook them in without declaring possibly conflicting static methods to import. This can be for new types (say, adding protobufs) or for new uses of existing types (say, Strings that are treated as Uris). This is the assertAbout() feature.

Other than that, Truth is very similar to AssertJ, since it was inspired by FEST, of which AssertJ is a fork of the 2.0 development line.

总而言之,Truth 的设计更具可扩展性和灵活性,但 AssertJ 将非常适合(可能是最强大的)标准类型断言。