开发者

Alternatives to partial mocking?

Tired of hand-crafting mocks, I am trying to introduce Mockito to my project.

Suppose I have a bunch of database accessors which isolate all Hibernate queries for given use case (or service). So I can have such interfaces as RoutingDao, UserDao, ReportingDao. Each of these interfaces extends a very generic Dao with methods such as commit(), rollback() or reload(Entity). With Hibernate the latter is supposed to reload a detached entity with the current session. In tests, it should just return its argument.

With hand-crafted mocks it was eas开发者_如何学编程y: I had a MockDao with common implementation of this method, and MockRoutingDao would extend MockDao and use that implementation. One way I see with Mockito is to create MockRoutingDao that extends MockDao and delegates unmocked calls to it. The not-inherited methods would be mocked with a spy(). But this is pretty ugly - I still need to hand-craft a mock, and the whole idea of partial mocking & inheritance for reusing some implementation is a mess.

How can I improve it? What are the best practices to create shared implementation for a mock method that can be reused accross many tests?


You really want to write an org.mockito.stubbing.Answer implementation that handles common stuff every *DAO mock will handle.

You can still add custom when stubbing to this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜