-
Notifications
You must be signed in to change notification settings - Fork 4
Create JUnit Tests (en)
The principle of the tests is that all JUnit tests stored in this file are checked.
The teacher must create several test cases for different input values for each method to be implemented by the students. This can indeed be a lot of typing, but it's worth it! Java then checks whether the result of the method implemented by the student matches the expected result. Therefore, most JUnit methods are based on the name assertEquals(a, b)
(which means: "Expect that the student's method (a) returns the same result as I expect (b).").
JUnit offers several methods that can be used. In my opinion, the most important ones are the following:
assertTrue(methodToCheck(...))
assertEquals(String expected, String got) // practically any data type can be used
assertArrayEquals(int[] expected, int[] got) // practically any array data type can be used
You can already see that the first method could also be omitted and instead the second one (assertEquals
) could be used with two boolean
types.
I would like to refer to the official JUnit page
Given the following task: The students are supposed to implement a method that finds the largest number in an int
array. If the array is empty, -1
should be returned.
The test cases should now definitely cover all special/unique cases:
- The input is an empty array
- The largest number is at the very front of the array
- The largest number is at the very end of the array
- The largest number is somewhere in the middle of the array
- There are negative numbers
- The largest number is negative itself
The test cases could now look like this:
@Test
public void task_01_Test1() {
ArrayExercise exercise = new ArrayExercise();
assertEquals(-1, exercise.largestNumber(new int[]{}));
}
@Test
public void task_01_Test2() {
ArrayExercise exercise = new ArrayExercise();
assertEquals(3, exercise.largestNumber(new int[]{3,2,1}));
}
@Test
public void task_01_Test3() {
ArrayExercise exercise = new ArrayExercise();
assertEquals(3, exercise.largestNumber(new int[]{1,2,3}));
}
@Test
public void task_01_Test4() {
ArrayExercise exercise = new ArrayExercise();
assertEquals(3, exercise.largestNumber(new int[]{1,3,2}));
}
@Test
public void task_01_Test5() {
ArrayExercise exercise = new ArrayExercise();
assertEquals(84, exercise.largestNumber(new int[]{-44,-279,84,13}));
}
@Test
public void task_01_Test6() {
ArrayExercise exercise = new ArrayExercise();
assertEquals(-10, exercise.largestNumber(new int[]{-10,-24,-199}));
}
It is recommended to open a new test method for each small test and to name and number these tests sensibly. This way, when errors occur during testing, students can see which test of which task failed. Although students do not see the exact implementation of the tests, they can still better estimate whether, for example, they are missing only a single edge case or whether they are completely on the wrong track. The tests are executed in order from top to bottom. So if the first error shown to the students is task_01_Test6, they know that only the sixth test for task 1 failed.