Functional Programming in Java, Second Edition: On p. 88 there is an occasion to show JUnit5 lambda support

On page 88, as a subchapter of “Separating Concerns Using Lambda Expressions”, one could bring up an example based on JUnit5 on how to use lambdas in an assertAll test, which takes a vararg of lambdas, and runs them all, failing the test as a whole if any of the lambdas (mapped to the interface org.junit.juipter.api.function.Executable) asserts false, as opposed to failing the test on the first assertion of false.

For example:

Starting off with not-a-real-test, which just prints:

    @Test
    public void totalsUsingUsingSelectingLambda() {
        System.out.println("Total of all assets: " + totalSelectableValues(assets, asset -> true));
        System.out.println("Total of all bonds: " + totalSelectableValues(assets, asset -> asset.isBond()));
        System.out.println("Total of all stocks: " + totalSelectableValues(assets, asset -> asset.isStock()));
    }

We can extend the above to a traditional “fail the test on the first assertion of false” test code:

    @Test
    public void actuallyTestTotalsUsingSelectingLambda() {
        int totalAssets = totalSelectableValues(assets, asset -> true);
        int totalBonds = totalSelectableValues(assets, asset -> asset.isBond());
        int totalStocks = totalSelectableValues(assets, asset -> asset.isStock());
        assertEquals(10000,totalAssets);
        assertEquals(3000,totalBonds);
        assertEquals(7000,totalStocks);
    }

But suppose we want to run all the asserts no matter what? with JUnit5 lambda support, we can do this, which relieves us from the need to write special code to attain the same functionality:

    @Test
    public void actuallyTestTotalAssetsBondsStocksUsingSelectingLambda() {
        int totalAssets = totalSelectableValues(assets, asset -> true);
        int totalBonds = totalSelectableValues(assets, asset -> asset.isBond());
        int totalStocks = totalSelectableValues(assets, asset -> asset.isStock());
        assertAll("totals",
                () -> assertEquals(10000,totalAssets),
                () -> assertEquals(3000,totalBonds),
                () -> assertEquals(7000,totalStocks)
        );
    }