Home  >  Article  >  Java  >  Why not provide JUnit's method assertNotEquals?

Why not provide JUnit's method assertNotEquals?

黄舟
黄舟Original
2016-12-29 09:14:011534browse

1. I suggest the new assertThat() style assertions, which makes it easy to describe various negations and automatically generates a description of what you expect if the assertions fail: assertThat(objectUnderTest,

is(not(someOtherObject)));
assertThat(objectUnderTest, not(someOtherObject));
assertThat(objectUnderTest, not(equalTo(someOtherObject)));

All three options are equivalent, pick the one you find most readable (and allow this tight syntax to work), you need these imports:

i

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

2. There is assertNotEquals in JUnit 4.11:
3. I don't know that the asserts API is not very suitable for testing the objects it provides and of course CodeGo.net, is this too Long write:

assertFalse(foo.equals(bar));


With the yield part this argument is so unfortunate it should be respectively:

String msg = "Expected <" + foo + "> to be unequal to <" + bar +">";
assertFalse(msg, foo.equals(bar));

That's of course makes it better to launch yourself assertNotEqual . Fortunately, in the future it will probably become part of JUnit: JUnit Issue 22
4. I think it's a neat thing to keep in mind if assertNotEqual does make JUnit a little less interesting to learn. In the case when adding it would reduce the API and at least help to govern the larger space. My guess is that the reason for the omission might be because there are too few multiplayer calls. However, I get one even when assertFalse doesn't exist. So there's a positive expectation that I might end up adding it, since it's not a difficult one, although I admit that there are workarounds that are even elegant. I'm very late to this party, though. I found that the following format:

static void assertTrue(java.lang.String message, boolean condition)

can work for most 'not equal' cases

int status = doSomething() ; // expected to return 123
assertTrue("doSomething() returned unexpected status", status != 123 ) ;

6. It is better to use Hamcrest negative assertions instead of assertFalse as in the test report. Displays a diff's assertions as failed. If assertFalse, you will only get a report of failed assertions.

7. The reason is that one would expect assertNotEquals() to be a built-in command without converting them first. For fully mature objects: Detailed examples:

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

and

assertNotEqual(1, winningBidderId);

8. Module API consistency, why JUnit does not provide assertNotEquals() is why JUnit always prefers it assertStringMatchesTheRegex(regex, str) vs. assertStringDoesntMatchTheRegex(regex, str) assertStringBeginsWith(prefix, str) vs. assertStringDoesntBeginWith(prefix, str) i.e. there is no end to providing a specific kind of logic for what you might want in your argument! Better to test primitives like equalTo(...), is(...), not(...), regex(...) and have these work together instead for more readability and sanity .


The above is why the JUnit method assertNotEquals is not provided? Content, for more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn