A failing test

A failing test is the normal state of a build in progress, and the log of a failure is the shortest lesson in reading one. The build below has one test with a wrong expected value, and both the run and the fix are shown.

One wrong assertion

settings.gradle

rootProject.name = 'shop'

build.gradle

plugins { id 'java' } repositories { mavenLocal() } dependencies { testImplementation 'org.junit.jupiter:junit-jupiter:5.14.3' testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.14.3' } tasks.named('test') { useJUnitPlatform() }

src/main/java/com/example/Calc.java

package com.example; public final class Calc { public static int add(int a, int b) { return a + b; } }

src/test/java/com/example/CalcTest.java

package com.example; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class CalcTest { @Test void addWorks() { assertEquals(5, Calc.add(2, 2)); } }

The failure

gradle test --console=plain --offline --no-daemon > build.log 2>&1; sed "s#$PWD#<project>#g" build.log

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses

> Task :test

CalcTest > addWorks() FAILED
    org.opentest4j.AssertionFailedError at CalcTest.java:10

1 test completed, 1 failed


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file://<project>/build/reports/tests/test/index.html

* Try:
> Run with --scan to get full insights from a Build Scan (powered by Develocity).

BUILD FAILED in 17s
3 actionable tasks: 3 executed

The exit status

A build that fails returns a status other than zero, which is what a CI job reads. Because the command above goes through a pipe, the status of the whole line is the status of sed, so a script that needs the status runs Gradle without the pipe:

gradle test --console=plain --offline --no-daemon > build.log 2>&1; echo "status: $?"; sed "s#$PWD#<project>#g" build.log

status: 1
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE

> Task :test

CalcTest > addWorks() FAILED
    org.opentest4j.AssertionFailedError at CalcTest.java:10

1 test completed, 1 failed


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file://<project>/build/reports/tests/test/index.html

* Try:
> Run with --scan to get full insights from a Build Scan (powered by Develocity).

BUILD FAILED in 15s
3 actionable tasks: 1 executed, 2 up-to-date

The three lines that matter in a failure are these. The test line CalcTest > addWorks() FAILED names the class and the method. The reason line names the exception and the file. The What went wrong block names the task and points at the HTML report, which is where the two values of a failed assertion are shown together.

The fix

The expected value was wrong. One character changes and the build passes:

src/test/java/com/example/CalcTest.java

package com.example; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; class CalcTest { @Test void addWorks() { assertEquals(4, Calc.add(2, 2)); } }

gradle test --console=plain --offline --no-daemon

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :compileTestJava
> Task :processTestResources NO-SOURCE
> Task :testClasses
> Task :test

BUILD SUCCESSFUL in 16s
3 actionable tasks: 2 executed, 1 up-to-date

Related

Article author: Arthur Isaev