What’s Important to Get Right When Using Mocks?
Learn the right way to use mock in your tests.
We'll cover the following
Best scenario
In the best case, we end up with a single-line arranged a portion of our test that creates an expectation using Mockito’s when(...).then(...)
construction. We have a single-line act and a single assert. These are tests we can quickly read, understand, and trust.
Tests using mocks should clearly state “what’s going on”.
Correlation
Tests using mocks should clearly state what is going on. One way we do this is by correlation. In answersAppropriateAddressForValidCoordinates
, it’s clear that the expected parameter string of lat=38.000000&lon=-104.000000
correlates to the act arguments of 38.0
and -104.0
. Things obviously aren’t always this easy, but the more we can help the test reader make that connection without digging through other code, the better our tests will be.
Don’t forget that mocks replace real behavior. We want to ask ourselves a few questions to make sure we’re using them properly.
Get hands-on with 1400+ tech skills courses.