Java - Cars, Assemble - provided tests improvement suggestion

I just worked through the Cars, Assemble exercise and encountered something that I think of as a unit test anti-pattern.

In the current implementation the assertions that compare doubles are in the form:
assertThat(Math.abs(carsAssemble.productionRatePerHour(9) - 1591.2) < epsilon).isTrue();
which means when the test fails the student gets an unhelpful message:
"Expecting: <false> to be equal to <true> but was not."

I thought it was standard to use the AreEquals double-with-delta form:
Assertions.assertEquals(1591.2, carsAssemble.productionRatePerHour(9), epsilon);
which reports the same failure in a more informative way:
Expected: 1591.2
Actual: 1989.0

The C# track does seem to have the more informative test pattern:
Assert.Equal(1591.2, AssemblyLine.ProductionRatePerHour(9), precision: 1);

I’ll make the changes if it’s worth updating - I did convert the tests locally when solving to get the added information anyways :wink:

1 Like

Yes please! That would definitely be very helpful.

Apologies for the delay, life & stuff…

Here’s the proposed change:

Note the difference in the epsilon boundary between current implementation and using assertEquals(double, double, epsilon). I don’t think it will cause any existing solutions to fail but honestly don’t remember the depths of double comparisons and if the existing epsilon is the right precision.
My gut is that the epsilon value doesn’t need to be 7 digits and for the values being calulcated in this exercise an epsilon of 3 or 4 digits would be precise enough.