Item 7: Make sure the test packages match the production code packages

By Ashley Waldron

All of the source code for this series can be found in this repository. Specifically, the code used throughout this item can be found here and its corresponding test classes found here.

At the end of the last item the code coverage tool showed 0% coverage for our Item5UserService class which might have been a bit surprising but it’s due to a simple enough reason:

Our Item6UserServiceTest test class is in the com.effective.unit.tests.service.item6 package but the Item5UserService class that we were testing is the com.effective.unit.tests.service.item5 package. The code coverage tools that I’ve worked with don’t actually register the code coverage unless the package that the test class is in is the same as the package that the target class is in .

This is easily fixed by moving Item5UserService.java into a package called com.effective.unit.tests.service.item6. Or moving the Item6UserServiceTest class into a the com.effective.unit.tests.service.item5 package. Once you do this you can run the full test class with your code coverage tool and should now show 100% code coverage.