After

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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.