No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
README.md

README.md

junit-practices

This project consists of the examples that demonstrates some good practices for using suitable assert variant for given scenario. For example if one wants to assert the string returned by a method is equal to an expected value, it can be done in two ways

  1. assertEquals(expected_string, actual_string)
 	@Test
	public void testStringWithAssertTrue() {
		assertTrue("abc".equals("vdc"));
	}
 	
  1. asserTure(expected_string.equals(actual_string))
 	@Test
	public void testStringWithAssertTrue() {
		assertTrue("abc".equals("vdc"));
	}

Both of these will serve the purpose of testing, but the question is will they give the perspective on a test failures? The test failure trace for the approach 1 is

java.lang.AssertionError
	at org.junit.Assert.fail(Assert.java:86)
	at org.junit.Assert.assertTrue(Assert.java:41)
	at org.junit.Assert.assertTrue(Assert.java:52)

failure trace for the approach 2 is

 org.junit.ComparisonFailure: expected:<[ab]c> but was:<[vd]c>
   at org.junit.Assert.assertEquals(Assert.java:115)
   at org.junit.Assert.assertEquals(Assert.java:144)

The second approach gives a better perspective of the failure; it clearly says whats expected and what is actual.