有人知道为什么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提供了我正在寻找的方法。我只是出于好奇才问的。
当前回答
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.
其他回答
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.
对于否定的断言,最好使用Hamcrest,而不是assertFalse,因为在前者中,测试报告将显示断言失败的差异。
如果使用assertFalse,只会在报告中得到一个断言失败。例如,丢失了关于失败原因的信息。
通常当我期望两个对象相等时,我这样做:
assertTrue(obj1.equals(obj2));
and:
assertFalse(obj1.equals(obj2));
当他们被认为是不平等的。我知道这不是对你问题的回答,但这是我能得到的最接近的答案。它可以帮助其他人搜索在JUnit 4.11之前的JUnit版本中可以做什么。
Modulo API一致性,JUnit不提供assertNotEquals()的原因与JUnit从未提供类似方法的原因相同
assertStringMatchesTheRegex(regex, str) vs. assertStringDoesntMatchTheRegex(regex, str) assertStringBeginsWith(prefix, str) vs. assertStringDoesntBeginWith(prefix, str)
也就是说,为断言逻辑中可能需要的东西提供特定的断言方法是无止境的!
更好的方法是提供可组合的测试原语,如equalTo(…)、is(…)、not(…)、regex(…),并让程序员将这些原语拼凑在一起,以获得更好的可读性和合理性。
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;