F[I]RST: [I]solate Your Tests

Good unit tests focus on a small chunk of code to verify. That’s in line with our definition of unit. The more code that your test interacts with, directly or indirectly, the more things are likely to go awry.

The code you’re testing might interact with other code that reads from a database. Data dependencies create a whole host of problems. Tests that must ultimately depend on a database require you to ensure that the database has the right data. If your data source is shared, you have to worry about external changes (maybe out of your control) breaking your tests. Don’t forget that other developers are often running their tests at the same time! Simply interacting with an external store increases the likelihood ...

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.