Junit5: assertEquals(expected, actual, delta) should allow a delta of zero

Created on 28 Sep 2018  路  4Comments  路  Source: junit-team/junit5

Overview

assertEquals(expected, actual, delta) currently throws an exception if a delta of zero is passed in.

This is a change in behavior from JUnit 4 and an unnecessary restriction. I propose that the desired behavior should be to treat delta of zero as a test for equality.

In my projects a delta of zero occurs when the test tolerance is dynamically computed. The current work around is to add EPS to the dynamic tolerance. This bloats the code and the desired test is for equality, not _nearly_ equal.

Proposal

Two simple modifications (once for float and double) are required. Change the line delta <= 0.0 to delta < 0.0.

    static void assertValidDelta(float delta) {
        if (Float.isNaN(delta) || delta < 0.0) {
            failIllegalDelta(String.valueOf(delta));
        }
    }

That's it.

Jupiter enhancement up-for-grabs

Most helpful comment

I'd be fine with changing it.

@junit-team/junit-lambda WDYT?

All 4 comments

I'd be fine with changing it.

@junit-team/junit-lambda WDYT?

+1

I've tried to fix this issue

_in progress_ in #1613

Was this page helpful?
0 / 5 - 0 ratings