Creating Mocks and Stubs
Before we use Mockito, note that we could write our own stub implementation, called, for example, InMemoryPersonRepository. Here’s an abbreviated version of that class (the complete version can be found in the GitHub repository):
| public class InMemoryPersonRepository implements PersonRepository { |
| private final List<Person> people = |
| Collections.synchronizedList(new ArrayList<>()); |
| |
| @Override |
| public final Person save(Person person) { |
| synchronized (people) { |
| people.add(person); |
| } |
| return person; |
| } |
| |
| @Override |
| public final void delete(Person person) { |
| synchronized (people) { |
| people.remove(person); |
| } |
| } |
| |
| // ... other methods from ... |
Get Mockito Made Clear 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.