title: JUnit - Using the Wrong Assert
body: <p>I quote this post from <a href="http://www.exubero.com/junit/antipatterns.html#Misuse_of_Assertions" rel="nofollow noreferrer">exubero's</a> entry. I think this entry will benefit everyone who is doing a unit test:</p>
  
  <p>There are a large number of different methods beginning with assert defined in Junit's Assert class. Each of these methods has slightly different arguments and semantics about what they are asserting. </p>
  
  <p>The following shows some irregular uses of assertTrue:</p>
  
  <pre><code>assertTrue("Objects must be the same", expected == actual);
  assertTrue("Objects must be equal", expected.equals(actual));
  assertTrue("Object must be null", actual == null);
  assertTrue("Object must not be null", actual != null);
  </code></pre>
  
  <p>Some unit testing experts pointed out that the above code could be better written as:</p>
  
  <pre><code>assertSame("Objects must be the same", expected, actual);
  assertEquals("Objects must be equal", expected, actual);
  assertNull("Object must be null", actual);
  assertNotNull("Object must not be null", actual);
  </code></pre>
  
  <p>One of the advantage of using the appropriate 'assertXXX()' will increase the readability of the unit test. Can anyone point out what other benefit of using the appropriate 'assertXXX()' ? </p>