有人知道为什么JUnit 4提供assertEquals(foo,bar)方法而不提供assertNotEqual(foo,bar)方法吗?
它提供了assertNotSame(对应于assertSame)和assertFalse(对应于assertTrue),所以它们没有包含assertnotqual似乎很奇怪。
顺便说一下,我知道JUnit-addons提供了我正在寻找的方法。我只是出于好奇才问的。
有人知道为什么JUnit 4提供assertEquals(foo,bar)方法而不提供assertNotEqual(foo,bar)方法吗?
它提供了assertNotSame(对应于assertSame)和assertFalse(对应于assertTrue),所以它们没有包含assertnotqual似乎很奇怪。
顺便说一下,我知道JUnit-addons提供了我正在寻找的方法。我只是出于好奇才问的。
当前回答
我建议你使用更新的assertThat()风格断言,它可以很容易地描述各种否定,并自动构建你所期望的描述以及如果断言失败你会得到什么:
assertThat(objectUnderTest, is(not(someOtherObject)));
assertThat(objectUnderTest, not(someOtherObject));
assertThat(objectUnderTest, not(equalTo(someOtherObject)));
这三个选项都是等效的,选择一个你觉得最易读的。
为了使用简单的方法名(并允许这种时态语法工作),你需要这些导入:
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
其他回答
人们想要assertNotEquals()的明显原因是比较内置对象,而不必首先将它们转换为完整的对象:
详细的例子:
....
assertThat(1, not(equalTo(Integer.valueOf(winningBidderId))));
....
vs.
assertNotEqual(1, winningBidderId);
不幸的是,由于Eclipse默认情况下不包括JUnit 4.11,您必须详细说明。
注意:我不认为'1'需要包装在Integer.valueOf()中,但由于我刚从。net返回,所以不要指望我的正确性。
通常当我期望两个对象相等时,我这样做:
assertTrue(obj1.equals(obj2));
and:
assertFalse(obj1.equals(obj2));
当他们被认为是不平等的。我知道这不是对你问题的回答,但这是我能得到的最接近的答案。它可以帮助其他人搜索在JUnit 4.11之前的JUnit版本中可以做什么。
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(,);它应该能解决你的问题
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.