| title | Exercises: Unit Testing |
|---|---|
| date | 2023-06-28 13:58:37 -0500 |
| draft | false |
| weight | 2 |
| originalAuthor | Sally Steuterman |
| originalAuthorGitHub | gildedgardenia |
| reviewer | |
| reviewerGitHub | |
| lastEditor | Terri Penn |
| lastEditorGitHub | tpenn |
| lastMod | 2023-09-05 05:05:00 -0700 |
Work on these exercises in the IntelliJ car-exercises
project in java-web-dev-projects/unit-testing/exercises. You will update your CarTest.java file by adding
more test cases.
Add a test for the third TODO, "gasTankLevel is accurate after driving within tank range".
-
Your test must use the
Carmethoddrive()test_car.drive(50);
-
With a value of
50miles passed intodrive(), we expecttest_carto have agasTankLevelof9.assertEquals(9, test_car.getGasTankLevel(), .001);
{{% expand "Check your solution" %}}
@Test
public void testGasTankAfterDriving() {
test_car.drive(50);
assertEquals(9, test_car.getGasTankLevel(),.001);
}{{% /expand %}}
Add a test for the fourth TODO, "gasTankLevel is accurate after attempting to drive past tank range".
- You're on your own for this one. You'll need to simulate the
Cartravelling farther than it'sgasTankLevelallows.
{{% expand "Check your solution" %}}
@Test
public void testGasTankAfterExceedingTankRange() {
test_car.drive(501);
assertEquals(0, test_car.getGasTankLevel(), .001);
}{{% /expand %}}
The test for our last TODO is a little different. We are going to perform an action on our car object, and we are expecting the object to throw an error. In this case, we are going to attempt to add gas to our car that exceeds the gas tank size.
-
First, we'll add some code to our test to tell JUnit to expect an exception.
//TODO: can't have more gas than tank size, expect an exception @Test() public void testGasOverfillException() { assertThrows(IllegalArgumentException.class, () -> ); }
This lets JUnit know that this test should pass if an
IllegalArgumentExceptionis thrown at any point during this test. -
Update the
Carclass to include anaddGas()method.public void addGas(double gas) { this.setGasTankLevel(gas + this.getGasTankLevel()); }
-
Back in
CarTest, implement the newaddGas()method and what should happen if the exception is thrown after the->syntax using this code:test_car.addGas(5), "Shouldn't get here, car cannot have more gas in tank than the size of the tank". The string will be displayed if the test fails. -
Run the test. It should fail! In the output is an unexpected exception. This test was expecting an
IllegalArgumentException, but it got anAssertionErrorexception. This caused the test to fail. -
We need to refactor
Carto throw an exception when too much gas is added to the tank. Find thesetGasTankLevelmethod and modify it:public void setGasTankLevel(double gasTankLevel) { if (gasTankLevel > this.getGasTankSize()) { throw new IllegalArgumentException("Can't exceed tank size"); } this.gasTankLevel = gasTankLevel; }
-
Now, run the test - it should pass!
{{% expand "Check your solution" %}}
@Test
public void testGasOverfillException() {
assertThrows(IllegalArgumentException.class, () -> test_car.addGas(5), "Shouldn't get here, car cannot have more gas in tank than the size of the tank"
);
}{{% /expand %}}