Two common challenges—multithreading and database interaction—are tough-enough topics on their own. Often many of your defects will come from code in these areas.

In general, you want to adhere to the following strategy for testing these more-difficult scenarios:

  • Separate concerns. Keep application logic apart from threading, database, or other dependencies causing you a problem. Isolate dependent code so it’s not rampant throughout your codebase.

  • Use mocks to break dependencies of unit tests on slow or volatile code.

  • Write integration tests where needed, but keep them simple and focused.

Next up: you’re almost ready to graduate. So far, you’ve focused on heads-down unit testing on your development machine. For the final chapter, you’ll ...

Get Pragmatic Unit Testing in Java 8 with JUnit now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.