11

有什么类似的assertThat(a, lessThan(b));吗?我目前正在使用 Junit 4.8.1,但我找不到小于。相反,我必须这样做assertTrue(a < b),但这有一个缺点,它不会在测试日志中打印两个数字。

4

2 回答 2

11

你试过 JUnit + Hamcrest吗?有关一些示例,请参阅此博客文章 - 它看起来几乎与您发布的内容完全相同:

JUnit 4 展示 – assertThat 和 Hamcrest Matchers

或者,还有来自JUnit-addons 项目的ComparableAssert

于 2013-03-10T23:54:21.583 回答
3

You can import Hamcrest like this and use the Matchers.lessThan() method.

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

assertThat(foo, Matchers.lessThan(bar));
于 2014-12-09T17:24:34.480 回答