有人知道为什么JUnit 4提供assertEquals(foo,bar)方法而不提供assertNotEqual(foo,bar)方法吗?

它提供了assertNotSame(对应于assertSame)和assertFalse(对应于assertTrue),所以它们没有包含assertnotqual似乎很奇怪。

顺便说一下,我知道JUnit-addons提供了我正在寻找的方法。我只是出于好奇才问的。


当前回答

JUnit 4.11中有一个assertNotEquals: https://github.com/junit-team/junit/blob/master/doc/ReleaseNotes4.11.md#improvements-to-assert-and-assume

import static org.junit.Assert.assertNotEquals;

其他回答

人们想要assertNotEquals()的明显原因是比较内置对象,而不必首先将它们转换为完整的对象:

详细的例子:

....
assertThat(1, not(equalTo(Integer.valueOf(winningBidderId))));
....

vs.

assertNotEqual(1, winningBidderId);

不幸的是,由于Eclipse默认情况下不包括JUnit 4.11,您必须详细说明。

注意:我不认为'1'需要包装在Integer.valueOf()中,但由于我刚从。net返回,所以不要指望我的正确性。

对于否定的断言,最好使用Hamcrest,而不是assertFalse,因为在前者中,测试报告将显示断言失败的差异。

如果使用assertFalse,只会在报告中得到一个断言失败。例如,丢失了关于失败原因的信息。

I'd argue that the absence of assertNotEqual is indeed an asymmetry and makes JUnit a bit less learnable. Mind that this is a neat case when adding a method would diminish the complexity of the API, at least for me: Symmetry helps ruling the bigger space. My guess is that the reason for the omission may be that there are too few people calling for the method. Yet, I remember a time when even assertFalse did not exist; hence, I have a positive expectation that the method might eventually be added, given that it is not a difficult one; even though I acknowledge that there are numerous workarounds, even elegant ones.

JUnit 4.11中有一个assertNotEquals: https://github.com/junit-team/junit/blob/master/doc/ReleaseNotes4.11.md#improvements-to-assert-and-assume

import static org.junit.Assert.assertNotEquals;

我在java 8环境下的JUnit工作,使用jUnit4.12

对我来说:编译器无法找到方法assertNotEquals,即使当我使用 进口org.junit.Assert;

所以我改变assertNotEquals("addb",字符串);assertNotEquals(“addb”字符串);

因此,如果您面临关于assertNotEqual不被识别的问题,则将其更改为Assert.assertNotEquals(,);它应该能解决你的问题