unit-testing-solution
branchtdd
Following TDD and the red-green-refactor workflow we need to understand our requirements before writing any code.
Today we will be implementing Range, and Trip Meter functionality.
The Range functionality will calculate how many more miles the car can drive before running out of gas. The output should be a Double representing the miles the car can drive before needing to fuel up.
The Trip Meter will be a separate resettable odometer. This will allow a user to track the number of miles travelled on a specific trip.
Based on the requirements we need to create three tests
@Test
public void testRangeOnInitialization() {
Car testCar = new Car("a", "a", 10, 20);
assertEquals(200, testCar.getRange());
}
@Test
public void testRangeAfterDriving() {
Car testCar = new Car("a", "a", 10, 20);
testCar.drive(100);
assertEquals(100, testCar.getRange());
}
@Test
public void testRangeAfterAddingGas() {
Car testCar = new Car("a", "a", 10, 20);
testCar.drive(200);
testCar.addGas(5.0);
assertEquals(100, testCar.getRange());
}
What happens when you run the tests?
Compilation Error! We are calling some methods that don’t yet exist. We will need to create these methods to get our tests to run. Remember we want to see a failing red test before a successful green test.
We need to add getRange()
to Car.java
.
public double getRange() {
return this.gasTankLevel;
}
After adding the code, run the tests again. Our tests should run now, and they should fail.
Let’s get our tests to pass:
public double getRange() {
return this.gasTankLevel * this.milesPerGallon;
}
Now we have introduced the proper logic that should return the correct range calculation. Let’s re-run those tests.
This time they are passing! In this case we don’t need to refactor. Our code is descriptive, efficient, and all of our tests are passing. Let’s move on.
Based on the requirements we need to create three tests
@Test
public void testInitialValue() {
Car testCar = new Car("a", "a", 10, 20);
assertEquals(0, testCar.getTripMeter());
}
@Test
public void testAfterDriving() {
Car testCar = new Car("a", "a", 10, 20);
testCar.drive(50);
assertEquals(50, testCar.getTripMeter());
}
@Test
public void testResetAfterDriving() {
Car testCar = new Car("a", "a", 10, 20);
testCar.drive(50);
testCar.resetTripMeter();
assertEquals(0, testCar.getTripMeter());
}
@Test
public void testResetThenDrive() {
Car testCar = new Car("a", "a", 10, 20);
testCar.drive(50);
testCar.resetTripMeter();
testCar.drive(25)
assertEquals(25, testCar.getTripMeter());
}
What happens when you run the tests?
Compilation error!
We will need to add some methods to our Car.java
class:
public double getTripMeter() {
return 0;
}
public void resetTripMeter() {
}
These methods are lacking for now, but should get us past our compilation errors.
We need to add a property tripMeter
and then update the methods we just added: getTripMeter()
and resetTripMeter()
to Car.java
.
Run the tests again. They should be passing now!
Do we need a refactor?