junit 5, gradle and containers

Up until recently, I’ve been using JUnit 5 with Eclipse and Maven. As I started using it with Gradle, I became frustrated that you couldn’t see the number of tests run when the tests succeeded. (Don’t worry; this is an easily solvable problem.)

First JUnit 4

You can add code to have Groovy print the number of tests. This matches the number of tests that Eclipse/Maven say are run.

test {
   afterSuite { desc, result ->
    	     if (!desc.parent)
        		println("${result.resultType} " +
            "(${result.testCount} tests, " +
            "${result.successfulTestCount} successes, " +
            "${result.failedTestCount} failures, " +
            "${result.skippedTestCount} skipped)")
   }
}

On to JUnit 5
It’s even less code to ask JUnit 5 to print the test results:

junitPlatform {
  details 'tree'
}

This prints something like

Test run finished after 2518 ms
 [ 20 containers found ]
 [ 0 containers skipped ]
 [ 20 containers started ]
 [ 0 containers aborted ]
 [ 20 containers successful ]
 [ 0 containers failed ]
 [ 27 tests found ]
 [ 1 tests skipped ]
 [ 26 tests started ]
 [ 0 tests aborted ]
 [ 26 tests successful ]
 [ 0 tests failed ]

I agree that I have 27 tests with 1 skipped. But what’s this 20 containers? As you can see from the tree, we have:

  • 1 – JUnit Vintage – to run the JUnit 3/4 tests
  • 1 – JUnit Jupiter – to run the JUnit 5 tests (I didn’t have any in the project at the time I captured this tree so it is an empty container; but still counts)
  • 9 – JUnit test classes
  • 9 – The nine data elements in my parameterized test.

├─ JUnit Vintage ✔
│ ├─ com.devnexus.workshop.junit5.CentennialOlympicParkTest ✔
│ │ ├─ oldestForFirstElement ✔
│ │ ├─ oldestForLastElement ✔
│ │ ├─ oldestForEmptyList ✔
│ │ └─ url ✔
│ ├─ com.devnexus.workshop.junit5.EarthquakeTimeoutTest ✔
│ │ └─ timeout ✔
│ ├─ com.devnexus.workshop.junit5.ParkBuildingTest ✔
│ │ ├─ equalsValues ✔
│ │ └─ hashCodeValues ✔
│ ├─ com.devnexus.workshop.junit5.EarthquakeTest ✔
│ │ ├─ usingStandalone ✔
│ │ ├─ noMessageChecking ✔
│ │ └─ usingRule ✔
│ ├─ com.devnexus.workshop.junit5.GeorgiaAquariumTest ✔
│ │ ├─ namesOfShows ✔
│ │ └─ numberLines ✔
│ ├─ com.devnexus.workshop.junit5.OlympicsDatesTest ✔
│ │ ├─ [1996-07-19] ✔
│ │ │ └─ date[1996-07-19] ✔
│ │ ├─ [1996-07-20] ✔
│ │ │ └─ date[1996-07-20] ✔
│ │ ├─ [1996-07-31] ✔
│ │ │ └─ date[1996-07-31] ✔
│ │ ├─ [1996-08-03] ✔
│ │ │ └─ date[1996-08-03] ✔
│ │ ├─ [1996-08-04] ✔
│ │ │ └─ date[1996-08-04] ✔
│ │ ├─ [1996-07-18] ✔
│ │ │ └─ date[1996-07-18] ✔
│ │ ├─ [1996-08-05] ✔
│ │ │ └─ date[1996-08-05] ✔
│ │ ├─ [1997-07-18] ✔
│ │ │ └─ date[1997-07-18] ✔
│ │ └─ [1995-08-01] ✔
│ │ └─ date[1995-08-01] ✔
│ ├─ com.devnexus.workshop.junit5.CentennialOlympicParkEnumTest ✔
│ │ └─ allClosingTimesAfter9pm ✔
│ ├─ com.devnexus.workshop.junit5.ShakeExceptionTest ✔
│ │ └─ message ↷ this test is ignored becuase it is unnecessary
│ └─ com.devnexus.workshop.junit5.GeorgiaAquariumWaitTest ✔
│ ├─ plentyOfTime ✔
│ ├─ exactOpening ✔
│ ├─ anyMinuteNow ✔
│ └─ alreadyOpen ✔
└─ JUnit Jupiter ✔

Test run finished after 2518 ms
[ 20 containers found ]
[ 0 containers skipped ]
[ 20 containers started ]
[ 0 containers aborted ]
[ 20 containers successful ]
[ 0 containers failed ]
[ 27 tests found ]
[ 1 tests skipped ]
[ 26 tests started ]
[ 0 tests aborted ]
[ 26 tests successful ]
[ 0 tests failed ]

 

Leave a Reply

Your email address will not be published. Required fields are marked *