Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't JUnit provide assertNotEquals methods?

Tags:

java

junit

assert

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.

like image 429
Chris B Avatar asked Jul 08 '09 07:07

Chris B


People also ask

How do you use assertNotEquals?

The assertNotEquals() method asserts that two objects are not equals. If they are, an AssertionError without a message is thrown. If unexpected and actual are null, they are considered equal. Let's first create Book, BookService classes, and then we will write JUnit test cases to use assertEquals() methods.

Is JUnit deprecated?

framework to org. junit. Assert in JUnit 4.0 - you can use that instead, it's not deprecated.

How do you assert null in JUnit?

Assert class in case of JUnit 4 or JUnit 3 to assert using assertNull method. Assertions. assertNull() checks that object is null. In case, object is not null, it will through AssertError.


2 Answers

I'd suggest you use the newer assertThat() style asserts, which can easily describe all kinds 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 simple names of the methods (and allow this tense syntax to work), you need these imports:

import static org.junit.Assert.*; import static org.hamcrest.CoreMatchers.*; 
like image 71
Joachim Sauer Avatar answered Sep 22 '22 06:09

Joachim Sauer


There is an assertNotEquals in JUnit 4.11: https://github.com/junit-team/junit/blob/master/doc/ReleaseNotes4.11.md#improvements-to-assert-and-assume

import static org.junit.Assert.assertNotEquals; 
like image 35
Stefan Birkner Avatar answered Sep 23 '22 06:09

Stefan Birkner