From the course: Programming Foundations: Test-Driven Development

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Mocking

Mocking - Java Tutorial

From the course: Programming Foundations: Test-Driven Development

Start my 1-month free trial

Mocking

- [Instructor] A mock object is a special kind of test double. Martin Fowler defines it as an object pre-programmed with expectations which form a specification of the calls they are expected to receive. Mock objects mock the expected behavior of an external dependency, and the emphasis here is on the behavior and not on it's state. Let us take an example. Let us say this object under test relies on an external DB to provide values for a set of keys. When the key is ABC, the expectation is that external DB will return the value nine. When it is DEF, then it will return seven and so on. To get a test double for certain external DB, I can create an external DB stub that has a meta get value. It takes the key as string and returns the appropriate value as expected. In tests, I'm testing my object under test that has a method total values that must add up all the values in the external DB. To make this happen, I use an instance of the stub in my object under test and invoke get value with…

Contents