java
java copied to clipboard
need-for-speed: Niptpick: Tests allow for a batteryDrain that's larger than the available battery.
First of all, Thank you for your work on such a lovely website.
Back on topic, I have some nitpicks with the need-for-speed
exercise.
Consider the following iterations 2 & 3
The tests will pass if we implement the batteryDrained
method either like so
public boolean batteryDrained() {
return battery <= 0;
}
Or, more correctly like so:
public boolean batteryDrained() {
return battery - batteryDrain < 0;
}
Imagine a Car that is initialized with a batteryDrain
larger than 50
. Logically this car should only be driven once, as the remaining battery is not enough to execute a full drive.
I'd consider adding a simple test that would check this logic like so:
@Test
@Tag("task:XX")
@DisplayName("Should only drive once")
public void new_remote_control_car_that_can_only_drive_once() {
var car = new NeedForSpeed(1, 99);
car.drive();
assertThat(car.batteryDrained()).isTrue();
// Consider then testing the drive further
car.drive();
assertThat(car.distanceDriven()).isEqualTo(1)
// Or maybe ensure that drive throws an exception
// assertThatExceptionOfType(SomeException.class).isThrownBy(() -> car.drive());
}