JUnit Assert is the most fundamental class in JUnit. In this post we’re going to show what are its abilities and how to use it correctly. Read on!
JUnit Assert CheatSheet
The following test shows how static helper methods from org.junit.Assert can be used to verify state of objects under the test:
package com.farenda.junit; import org.junit.Test; import static java.util.Collections.emptyList; import static org.junit.Assert.*; public class AssertionsTest { @Test public void shouldAssertEqualElements() { assertEquals(3, 3); // integers assertEquals(3.14, 3.14, 0.0001); // compare doubles with delta! assertEquals("Java", "Java"); // objects assertEquals("The descriptive message!", 3, 3); // with message } @Test public void shouldAssertArrayEquality() { assertArrayEquals(new int[]{1, 2, 3}, new int[]{1, 2, 3}); assertArrayEquals("Those tables!", new boolean[]{true}, new boolean[]{true}); } @Test public void shouldShowInequality() { assertNotEquals(3, 2+2); assertNotEquals("Close, but no.", 3.14, 3.1415, 0.0001); } @Test public void shouldAssertBooleanResults() { assertTrue(emptyList().isEmpty()); assertTrue("It should be empty", emptyList().isEmpty()); assertFalse("Yoda".isEmpty()); assertFalse("There should be only one!", "Yoda".isEmpty()); } @Test public void shouldTestNullness() { assertNull(null); assertNull("This one have to be null", null); assertNotNull("Han Solo"); assertNotNull("Should not happen!", new Object()); } @Test public void shouldFindTheSameObject() { String jedi = "Luke S"; assertSame("There's only one Jedi!", jedi, jedi); assertNotSame("These guys are different!", new Object(), new Object()); } }
Important thing to note is that double and float numbers have to be compared with some accuracy, due to limitations of computers to represent numbers. In our case accuracy of 0.00001 is more than enough.
How pros do it
As in the previous post we’ll take one example from Facebook Fresco:
package com.facebook.common.util; import org.junit.Test; import static org.junit.Assert.*; /** Unit test for {@link TriState}. */ public class TriStateTest { @Test public void testValueOf() { assertEquals(TriState.YES, TriState.valueOf(true)); assertEquals(TriState.NO, TriState.valueOf(false)); } // ... @Test public void testAsBooleanValidValues() { assertTrue(TriState.YES.asBoolean()); assertFalse(TriState.NO.asBoolean()); } // ... @Test public void testAsBooleanObject() { assertSame(Boolean.TRUE, TriState.YES.asBooleanObject()); assertSame(Boolean.FALSE, TriState.NO.asBooleanObject()); assertNull(TriState.UNSET.asBooleanObject()); } }
Another one from Spring Framework Base64UtilsTests:
package org.springframework.util; import org.junit.Test; import static org.junit.Assert.*; /** * @author Juergen Hoeller * @since 4.2 */ public class Base64UtilsTests { // ... other tests removed for brevity @Test public void encodeDecodeUrlSafe() { byte[] bytes = new byte[] { (byte) 0xfb, (byte) 0xf0 }; assertArrayEquals("-_A=".getBytes(), Base64Utils.encodeUrlSafe(bytes)); assertArrayEquals(bytes, Base64Utils.decodeUrlSafe(Base64Utils.encodeUrlSafe(bytes))); assertEquals("-_A=", Base64Utils.encodeToUrlSafeString(bytes)); assertArrayEquals(bytes, Base64Utils.decodeFromUrlSafeString(Base64Utils.encodeToUrlSafeString(bytes))); } }
Happy testing! :-)