Mockito

Simple Stub Creation with Mockito Partial Mocks

3 minute read Published:

When testing code that interacts with lower layers, I often stub out the lower layer to return objects to the code that I’m actually testing. I mostly use stubs generated by a mocking framework, but I occasionally come across cases where the stub needs to do something other than just return a known object. Take the case of a DAO that assigns a generated identifier to a domain object. Here we see a service that takes a name, creates a User object with it, persists the object and returns the generated id to the caller.