为什么 JUnit 不提供 assertNotEquals 方法? [英] Why doesn't JUnit provide assertNotEquals methods?
问题描述
有人知道为什么 JUnit 4 提供 assertEquals(foo,bar)
而不是 assertNotEqual(foo,bar)
方法吗?
Does anybody know why JUnit 4 provides assertEquals(foo,bar)
but not assertNotEqual(foo,bar)
methods?
它提供了assertNotSame
(对应于assertSame
)和assertFalse
(对应于assertTrue
),所以看起来奇怪的是,他们没有打扰包括 assertNotEqual
.
It provides assertNotSame
(corresponding to assertSame
) and assertFalse
(corresponding to assertTrue
), so it seems strange that they didn't bother including assertNotEqual
.
顺便说一下,我知道 JUnit 插件提供了我正在寻找的方法.我只是出于好奇而问.
By the way, I know that JUnit-addons provides the methods I'm looking for. I'm just asking out of curiosity.
推荐答案
我建议你使用较新的 assertThat()
风格的断言,它可以很容易地描述各种否定,并自动构建你所期望的以及断言失败时你得到的描述:
I'd suggest you use the newer assertThat()
style asserts, which can easily describe all kinds of negations and automatically build a description of what you expected and what you got if the assertion fails:
assertThat(objectUnderTest, is(not(someOtherObject)));
assertThat(objectUnderTest, not(someOtherObject));
assertThat(objectUnderTest, not(equalTo(someOtherObject)));
所有三个选项都是等效的,请选择您认为最易读的一个.
All three options are equivalent, choose the one you find most readable.
要使用方法的简单名称(并允许这种紧张的语法起作用),您需要这些导入:
To use the simple names of the methods (and allow this tense syntax to work), you need these imports:
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
这篇关于为什么 JUnit 不提供 assertNotEquals 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!