JUnit Testing
Contents
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 testfail(String message)
- instantly fail a test with a message1 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.
|
|