Skip to content

Commit ca2d2bc

Browse files
authored
Update CarsAssembleTest.java (#2521)
* Update CarsAssembleTest.java Tests for Task 1 used a boolean comparison to determine whether tests pass. Replaced those with more idiomatic assertEquals(double, double, epsilon) form. When the new style of test fails the user is shown the expected value and the actual value: Expected :1591.2 Actual :1989.0 The previous implementation showed the user the less clear: Expected :true Actual :false The test implementation being replaced required the comparison to be strictly less than epsilon, not inclusive of epsilon. The JUnit assertEquals replacement check is inclusive of epsilon. OLD: Math.abs(value1 - value2) < epsilon) NEW: Math.abs(value1 - value2) <= epsilon Given an epsilon of 0.0000001d this is unlikely to see any existing solutions fail given the expected answers only check the tenths place. * Update CarsAssembleTest.java Use AssertJ isCloseTo style assertion instead of JUnit assertEquals.
1 parent 1e22361 commit ca2d2bc

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

exercises/concept/cars-assemble/src/test/java/CarsAssembleTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.junit.jupiter.api.Test;
55

66
import static org.assertj.core.api.Assertions.*;
7+
import static org.assertj.core.api.Assertions.within;
78

89

910
public class CarsAssembleTest {
@@ -20,42 +21,42 @@ public void setUp() {
2021
@Tag("task:1")
2122
@DisplayName("The productionRatePerHour method returns the correct result when line's speed is 0")
2223
public void productionRatePerHourForSpeedZero() {
23-
assertThat(Math.abs(carsAssemble.productionRatePerHour(0) - 0.0) < epsilon).isTrue();
24+
assertThat(carsAssemble.productionRatePerHour(0)).isCloseTo(0.0, within(epsilon));
2425
}
2526

2627
@Test
2728
@Tag("task:1")
2829
@DisplayName("The productionRatePerHour method returns the correct result when line's speed is 1")
2930
public void productionRatePerHourForSpeedOne() {
30-
assertThat(Math.abs(carsAssemble.productionRatePerHour(1) - 221.0) < epsilon).isTrue();
31+
assertThat(carsAssemble.productionRatePerHour(1)).isCloseTo(221.0, within(epsilon));
3132
}
3233

3334
@Test
3435
@Tag("task:1")
3536
@DisplayName("The productionRatePerHour method returns the correct result when line's speed is 4")
3637
public void productionRatePerHourForSpeedFour() {
37-
assertThat(Math.abs(carsAssemble.productionRatePerHour(4) - 884.0) < epsilon).isTrue();
38+
assertThat(carsAssemble.productionRatePerHour(4)).isCloseTo(884.0, within(epsilon));
3839
}
3940

4041
@Test
4142
@Tag("task:1")
4243
@DisplayName("The productionRatePerHour method returns the correct result when line's speed is 7")
4344
public void productionRatePerHourForSpeedSeven() {
44-
assertThat(Math.abs(carsAssemble.productionRatePerHour(7) - 1392.3) < epsilon).isTrue();
45+
assertThat(carsAssemble.productionRatePerHour(7)).isCloseTo(1392.3, within(epsilon));
4546
}
4647

4748
@Test
4849
@Tag("task:1")
4950
@DisplayName("The productionRatePerHour method returns the correct result when line's speed is 9")
5051
public void productionRatePerHourForSpeedNine() {
51-
assertThat(Math.abs(carsAssemble.productionRatePerHour(9) - 1591.2) < epsilon).isTrue();
52+
assertThat(carsAssemble.productionRatePerHour(9)).isCloseTo(1591.2, within(epsilon));
5253
}
5354

5455
@Test
5556
@Tag("task:1")
5657
@DisplayName("The productionRatePerHour method returns the correct result when line's speed is 10")
5758
public void productionRatePerHourForSpeedTen() {
58-
assertThat(Math.abs(carsAssemble.productionRatePerHour(10) - 1701.7) < epsilon).isTrue();
59+
assertThat(carsAssemble.productionRatePerHour(10)).isCloseTo(1701.7, within(epsilon));
5960
}
6061

6162
@Test

0 commit comments

Comments
 (0)