Testing

Table of Contents

Tests are how programmers know their program works. Knowing that your code is completely correct is usually impossible, but passing tests provide strong evidence. Test-driven development is a type of development style where test are written first, then code.

1. Unit Testing

Unit testing is a software testing method where individual units of code (such as functions) are tested to determine whether or not they do what is expected of them. Since ad-hoc testing is repetitive and tedious, we often use unit testing frameworks to abstract this away, such as Truth:

import static com.google.common.truth.Truth.assertThat;

public class TestSort {
    @Test
    public void testSort() {
        String[] input = {"string", "apple", "helloworld", "wobbly"};
        String[] expected = {"apple", "helloworld", "string", "wobbly"};

        Sort.sort(input);
        assertThat(input).isEqualTo(expected);
    }
}
Example: Selection sort

Selection sorts a list of N items by:

  1. Find the smallest item.
  2. Move it to the front.°
  3. Selection sort the remaining N-1 items (without touching the first item).

First, we'll try implementing a function findSmallest(), that finds the smallest element in a list. To demonstrate test-driven development (TDD), we'll begin by writing a test that shows what we expect this function to do:

@Test
public void testFindSmallest() {
    String[] input = {"string", "apple", "helloworld", "wobbly"};

    int expected1 = 1;
    int actual1 = findSmallest(input, 0);
    assertThat(actual1).isEqualTo(expected1);

    int expected2 = 2;
    int actual2 = findSmallest(input, 2);
    assertThat(actual2).isEqualTo(expected2);
}

Next, we'll write the implementation for findSmallest:

public static int findSmallest(String[] input, int start) {
    int smallestSoFarIndex = start;
    for (int i = start; i < input.length; i += 1) {
        int comparisonResult = input[i].compareTo(input[smallestSoFarIndex]);
        if (comparisonResult < 0) {
            smallestSoFarIndex = i;
        }
    }
    return smallestSoFarIndex;
}

Next, we want to be able to swap two elements in an array. Again, we'll write the test first:

@Test
public void testSwap() {
    String[] input = {"string", "apple", "helloworld", "wobbly"};
    String[] expected  = {"string", "apple", "wobbly", "helloworld"};
    assertThat(swap(input, 2, 3)).isEqualTo(expected);
}

And then the implementation:

public static void swap(String[] input, int a, int b) {
    String temp = input[a];
    input[a] = input[b];
    input[b] = temp;
}

Finally, we're ready to write the implementation of selection sort. Here's the test we've written before this example again:

@Test
public void testSort() {
    String[] input = {"string", "apple", "helloworld", "wobbly"};
    String[] expected = {"apple", "helloworld", "string", "wobbly"};

    Sort.sort(input);
    assertThat(input).isEqualTo(expected);
}

And now is our final implementation:

public static void sort(String[] input) {
    sort(input, 0);
}

private static void sort(String[] input, int start) {
    int smallest = findSmallest(input, start);
    swap(input, 0, smallest);
    sort(input, start + 1);
}
Last modified: 2026-01-28 14:31