Learn how code coverage tools help guide you as you write pin-down tests. In this video, learn how to run your tests using code coverage tools. Identify lines of code that your tests are not executing and then construct new tests that hit those lines of code.
- I've gone ahead an added additional pin-down tests based on the requirements document. I encourage you to do the same before you watch this video. And I've included a copy of my codes so you can begin this video with the same code as me. The next step in pinning down the behavior of the legacy system is to look at test code coverage. To do this, we'll run Gilded Rose test with coverage. Up in the right hand corner of the IntelliJ window, click "Run with code coverage".
So a summary table pops up, showing us the code coverage of our tests. Double click on com.gildedrose, and we see the summary results for the two classes in our production code. Gilded Rose has 93% code coverage, Item has 83% code coverage. We haven't looked at Item yet, so lets open up Item.java. On the left hand side of the source code window, we see this green marking from lines 11 through 15.
That means that our tests have touched these lines of code. Scroll down a little bit, and at line 18 we see this red marker. That means out tests have not touched this line of code. Now this line of code is a two-string method, it's overriding the default two-string. I can make a judgment call here and say we don't need to write a test that executes this line of code. Lets go back to our Gilded Rose source code. We've got lots of green markings on the left hand side so our tests have touched most of this code.
Scrolling down. Looks like there are only two lines of code that haven't been touched yet. I'll close the coverage summary window. We'll take a look at these lines of code. Now our task here is to figure out how to construct a test that touches these lines of code. Going to scroll up a little bit to see if we can figure this out. It looks like at line 43 we're looking for an item with a sellin less than zero, and with the item type Aged Brie.
And that when we hit that, the quality will go up by one. Lets try to write a test that reaches lines 55 and 56. So back in Gilded Rose test. We'll add this new test. @Test. Public void. Give it a name like "Aged Brie never expires", I think that's what that code is trying to do. Create a new item.
With the name "Aged Brie". With a sellin of zero. I think that will get us into that code. And a quality of 42. And what we're expecting is that the sellin stays zero, so it never expires. And that the quality goes up because the quality of Aged Brie always goes up.
We'll run these tests and see what we get for the test result. Well it went red, let's figure out why it went red. It says expected zero, actual was negative one. Okay, so we know that the actual behavior is that when the sellin is zero for Aged Brie, it still decreases by one. Put a negative one there at line 90. Now this is different from test driven development of a new feature.
This test technically is red, but instead of fixing the production code, actually there's nothing to fix here, we're writing a new pin-down test that's helping us understand the existing legacy code. So we'll just update the test and re-run it. Alright, it's still red. Lets examine why. Expected 43 and actual was 44. Okay, it turns out that, in this case, the quality of Aged Brie increases by two instead of by one.
So lets pin-down that behavior and re-run the tests. Okay we're all green again, so this test pins down that Aged Brie behavior. And lets run with code coverage again, and see if we hit those lines of code. Go back to the GildedRose.java source file. We're trying hit lines 55 and 56. They're now marked in green. So we've successfully written a new pin-down test that reaches those lines of code.
- Defining refactoring
- Setting up a test-driven development environment
- Renaming variables
- Using the extract method
- Pinning down legacy code
- Testing code and branch coverage
- Refactoring legacy code
- Simplifying math and Booleans
- Adding new behaviors to refactored code