THINK FIRST·CODE LATER

← All labs

Critique the AI: a generated JUnit test class

Problem

Daniel asked an AI assistant to "write thorough JUnit tests" for LatePenalty.scoreAfterLatePenalty(int score, int daysLate) (rules: full score if ≤ 0 days late; −10% per day for 1–7 days; 0 after 7 days; rounded to nearest integer; negative scores throw IllegalArgumentException). It produced:

public class LatePenaltyTest {
    static int last;

    @Test
    public void test1() {
        last = LatePenalty.scoreAfterLatePenalty(100, 3);
        assertEquals(last, LatePenalty.scoreAfterLatePenalty(100, 3));
    }

    @Test
    public void test2() {
        assertEquals(LatePenalty.scoreAfterLatePenalty(100, 1), 90);
        assertTrue(last > 0);
    }

    @Test
    public void testNegative() {
        try {
            LatePenalty.scoreAfterLatePenalty(-5, 1);
        } catch (Exception e) {
            // expected
        }
    }

    @Test
    public void testManyValues() {
        for (int d = 1; d < 7; d++) {
            int r = LatePenalty.scoreAfterLatePenalty(100, d);
            assertNotNull(r);
        }
    }

    @Test
    public void testToday() {
        int days = (int) ChronoUnit.DAYS.between(LocalDate.of(2026, 10, 1), LocalDate.now());
        assertTrue(LatePenalty.scoreAfterLatePenalty(80, days) >= 0);
    }
}
  1. Find at least seven problems (name the test, the problem, and the FIRST property or principle violated).
  2. Rewrite the class as a good test suite: behaviour-based names, Arrange–Act–Assert, expected values from the rules, boundaries (0, 1, 7, 8 days), rounding, invalid input with assertThrows, no shared state.
  3. Explain how you would check that your new suite is actually strong.

Work it out on paper, in a document or here, then compare with the model answer. Your answer stays in your browser — it is never sent to or stored on the server.