Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (4k points)
Does anybody know why JUnit 4 provides assertEquals(foo,bar) but not assertNotEqual(foo,bar) methods?

It provides assertNotSame (corresponding to assertSame) and assertFalse (corresponding to assertTrue), so it seems strange that they didn't bother including assertNotEqual.

By the way, I know that JUnit-addons provides the methods I'm looking for. I'm just asking out of curiosity.

java JUnit assert

1 Answer

0 votes
by (46k points)

I recommend you use the latest assertThat() style asserts, which can simply describe all sort 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 easy names of the methods (and let this stiff syntax to work), you require these imports:

import static org.junit.Assert.*;

import static org.hamcrest.CoreMatchers.*;

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.7k users

Browse Categories

...