TL;DR: Testing is important!

In Java, Unit Tests are commonly provided by the JUnit library

Have a look at this cheat-sheet:

Source: https://jrebel.com/rebellabs/junit-cheat-sheet/


  • Each test should be independent, and not rely on each other

  • Each test should have some variant of a test decorator

    1
    2
    3
    4
    
    @Test
    void myCoolTest() {
    // doStuff();
    }
  • fail() - instantly fail a test

  • fail(String message) - instantly fail a test with a message

    1
    2
    3
    4
    5
    6
    7
    
    @Test
    void doTheThing() {
    // doMoreStuff()
    if (result != 6) {
    fail();
    }
    }
  • assertTrue()

  • assertFalse()

  • assertEqual(expected, value)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    void checkThatThing() {
    assertTrue(obj.hasThingy());
    assertFalse(obj.isRound());
      
    assertEqual(6, obj.getValue());
    assertTrue(obj.getValue() == 6); // Also the same
    
    // Though as usual, String comparisons _require_ the .equals() construction
    assertEqual("Pan", obj.getText());
    // FAIL (probably): assertTrue("Pan" == obj.getText());
    }

Parameterised Testing

You can systematically supply arguments into your test cases. These arguments can be sourced from an iterable value in the testing file.

1
2
3
4
5
6
7
8
9
@ParamaterisedTest
@MethodSoure(value = "data")
void test(int arg) {
  // doStuff();
}

public static int[] data() {
  return new int[] {5,3,10,2,20,42 };
}