COR[R]ECT: [R]eference

When testing a method, consider:

  • What it references outside its scope

  • What external dependencies it has

  • Whether it depends on the object being in a certain state

  • Any other conditions that must exist

A web app that displays a customer’s account history might require the customer to be logged on. The pop() method for a stack requires a nonempty stack. Shifting your car’s transmission from Drive to Park requires you to first stop—if your transmission allowed the shift while the car was moving, it’d likely deliver some hefty damage to your fine Geo Metro.

When you make assumptions about any state, you should verify that your code is reasonably well-behaved when those assumptions are not met. Imagine you’re developing the ...

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.