Red, fix, green

A wrong expectation and a wrong program produce the same red report, and the previous lesson fixed the expectation. This lesson does the other case: the tests are right, the class has a bug, and the report says which call returned the wrong value. Then the bug is fixed in the class, and the same tests turn green without an edit.

The class and three tests

Halves.java has one method that returns half of an integer. Integer division truncates, so half of 5 is 2 and half of 10 is 5. Three tests cover an even number, an odd number and ten.

public class Halves { static int half(int n) { return n / 2; } }

import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class HalvesTest { @Test void halvesAnEvenNumber() { assertEquals(3, Halves.half(6)); } @Test void roundsDownAnOddNumber() { assertEquals(2, Halves.half(5)); } @Test void halvesTen() { assertEquals(4, Halves.half(10)); } }

The third test is wrong on purpose: half of ten is five, not four. The first two tests pass and the third fails, so the run is red with two successful tests and one failure.

The red run

javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out Halves.java HalvesTest.java
junit execute --class-path out --scan-classpath --disable-ansi-colors

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│  └─ HalvesTest ✔
│     ├─ halvesAnEvenNumber() ✔
│     ├─ roundsDownAnOddNumber() ✔
│     └─ halvesTen() ✘ expected: <4> but was: <5>
└─ JUnit Vintage ✔

Failures (1):
  JUnit Jupiter:HalvesTest:halvesTen()
    MethodSource [className = 'HalvesTest', methodName = 'halvesTen', methodParameterTypes = '']
    => org.opentest4j.AssertionFailedError: expected: <4> but was: <5>
       org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
       org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
       org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
       org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:150)
       org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:145)
       org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:531)
       HalvesTest.halvesTen(HalvesTest.java:18)
       java.base/java.lang.reflect.Method.invoke(Method.java:580)
       java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
       java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Test run finished after 778 ms
[         4 containers found      ]
[         0 containers skipped    ]
[         4 containers started    ]
[         0 containers aborted    ]
[         4 containers successful ]
[         0 containers failed     ]
[         3 tests found           ]
[         0 tests skipped         ]
[         3 tests started         ]
[         0 tests aborted         ]
[         2 tests successful      ]
[         1 tests failed          ]

Why the test is right

Before changing anything, read the message against the method. The test expected 4 and the method returned 5, and Halves.half(10) is 10 / 2, which is 5. The class is correct and the test is wrong, so the fix belongs in the test.

This is the case the previous lesson warned about: a red report does not say who is wrong. The person has to decide, and the deciding argument is the method's contract, not the assertion. Here the method returns half of its argument, and half of ten is five.

The fix

Change assertEquals(4, Halves.half(10)) to assertEquals(5, Halves.half(10)). The class under test does not change; the test file does. The next block is the whole test file after the edit:

import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class HalvesTest { @Test void halvesAnEvenNumber() { assertEquals(3, Halves.half(6)); } @Test void roundsDownAnOddNumber() { assertEquals(2, Halves.half(5)); } @Test void halvesTen() { assertEquals(5, Halves.half(10)); } }

The green run

javac -Xlint:all -cp /opt/java-tools/junit-platform-console-standalone.jar -d out Halves.java HalvesTest.java
junit execute --class-path out --scan-classpath --disable-ansi-colors

Thanks for using JUnit! Support its development at https://junit.org/sponsoring

╷
├─ JUnit Platform Suite ✔
├─ JUnit Jupiter ✔
│  └─ HalvesTest ✔
│     ├─ halvesAnEvenNumber() ✔
│     ├─ roundsDownAnOddNumber() ✔
│     └─ halvesTen() ✔
└─ JUnit Vintage ✔

Test run finished after 428 ms
[         4 containers found      ]
[         0 containers skipped    ]
[         4 containers started    ]
[         0 containers aborted    ]
[         4 containers successful ]
[         0 containers failed     ]
[         3 tests found           ]
[         0 tests skipped         ]
[         3 tests started         ]
[         0 tests aborted         ]
[         3 tests successful      ]
[         0 tests failed          ]

The loop

The loop is small and worth naming: write a test, run it, read the first failing line, make one change, run again. The report tells you which test and which line; it does not tell you whether the class or the test is wrong, and that judgement is the work. When the tests are right, the same loop also drives the code: write the test before the method, watch it fail because the method is missing, then write the method until the report is green.

The next lessons take the assertions one kind at a time, so that a failure message can be read as easily for strings and arrays as for an integer.

Exercises

  1. Fix the test by changing the expected value, run it, and copy the two counts lines that changed.
  2. Change the class instead, so that half(10) returns 4. Does the test pass? Is the class now correct for the other two tests?
  3. Add a fourth test for half(-6) and predict the result of integer division for a negative number before you run it.
  4. Delete one test and run. Which container count changes, and which does not?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. A red report says expected: <4> but was: <5>. What does the test believe?

    Answer

    The method should have returned 4

    The first value is the expected one, written by the test; the second is what the call returned.

  2. Who decides whether the class or the test is wrong?

    Answer

    The person, from the method contract and the expected result

    The report names the mismatch; the contract of the method decides which side to change.

  3. What changes in the report when the red test is fixed?

    Answer

    The method gets a check mark, the failure list disappears, and tests failed becomes 0

    The same tests now all succeed, so the failure list is empty and the failed count is 0.

  4. What does this program print? public class Q1 { public static void main(String[] args) { System.out.println(10 / 2); } }

    Answer

    5

    Integer division returns the whole part, and 10 divided by 2 is 5.

  5. What does this program print? public class Q2 { public static void main(String[] args) { System.out.println(5 / 2); } }

    Answer

    2

    Integer division truncates toward zero, so 5 / 2 is 2.

  6. What does this program print? public class Q3 { public static void main(String[] args) { int n = 6; n = n - 1; System.out.println(n); } }

    Answer

    5

    The assignment replaces 6 with 5, and println prints the new value.

  7. What does this program print? public class Q4 { public static void main(String[] args) { System.out.println("half: " + 10 / 2); } }

    Answer

    half: 5

    Division happens before the string is joined, so the result is half: 5.

  8. The tests are green and the code has a bug the tests do not cover. What does the report say?

    Answer

    Nothing: a green run only says the written tests passed

    A test proves what it checks, and nothing else; missing cases stay invisible to the report.

  9. What does this program print? public class Q5 { public static void main(String[] args) { String text = "green"; System.out.println(text.length()); } }

    Answer

    5

    The string has five characters, so length returns 5.

  10. What is the smallest change that turns this test green? assertEquals(4, Halves.half(10))

    Answer

    Change 4 to 5

    Half of ten is five, so the expected value is the part that is wrong.

The code on this page was built with OpenJDK 21.0.13 (javac) and run with JUnit Platform Console Launcher 1.14.3 on Alpine Linux in a Docker container.

Article author: Arthur Isaev