QuickTip (Spock): Stubbing with null
This is less of a problem and more of a code style thing, but I noticed recently that some of our Spock code has several instances of >> null.
For example:
It's nice to see that the interactions are written in the same style, but for many methods1, Spock returns null by default so writing the >> null is redundant and I prefer to remove it.
Do you agree or not? Feel free to comment below. If this is something we agree on, perhaps it should be a new codenarc rule.
For example:
...
then:
result
1 * service.doSomething() >> new Blah()
1 * service.doSomethingElse() >> null
...
It's nice to see that the interactions are written in the same style, but for many methods1, Spock returns null by default so writing the >> null is redundant and I prefer to remove it.
Do you agree or not? Feel free to comment below. If this is something we agree on, perhaps it should be a new codenarc rule.
Since 1.2 Spock became even smarter with default mock return values. E.g. return Optional.empty instead of null. So the rule should not be "don't write `>> null` but "don't write `>> whateverIsDefault`".
ReplyDeleteGreat feedback. Thanks!
Delete