I'd like to mock some fluent interface with mock, which is basically a mail builder:
this.builder()
.from(from)
.to(to)
.cc(cc)
.bcc(bcc)
.template(templateId, templateParameter)
.send();
When mocking this with Spock, this needs a lot of setup like this:
def builder = Mock(Builder)
builder.from(_) >> builder
builder.to(_) >> builder
etc. It becomes even more cumbersome when you want to test certain interactions with the mock, depending on the use case. So I basically got two questions here:
- Is there a way to combine mock rules, so that I could do a general one time setup of the fluent interface in a method that I can reuse on every test case and then specify additional rules per test-case?
Is there a way to specify the mocking of a fluent interface with less code, e.g. something like:
def builder = Mock(Builder) builder./(from|to|cc|bcc|template)/(*) >> builder
or something equivalent to Mockito's Deep Stubs (see http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#RETURNS_DEEP_STUBS)