Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

JUnit Assert CheatSheet

farenda 2015-12-24 0

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! :-)

Share with the World!
Categories JUnit Tags java, junit, unit-tests
Previous: JUnit Getting Started
Next: JUnit fixtures – code reuse

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2021

sponsored