0

Have a look at this Spock test which executes fine, but tests multiple things in a single test method.

class WorkbookPoolSpec extends Specification {
    def 'test pool'() {
        setup:
            def workbook = Mock(Workbook)
            def workbookFactory = Mock(WorkbookFactory)
            def workbookPool = new WorkbookPool(workbookFactory)

        when:
            workbookPool.borrowWorkbook()
        then:
            1 * workbookFactory.create() >> workbook

        when:
            workbookPool.returnWorkbook(workbook)
        then:
            1 * workbook.reset()
    }
}

In reality my test is much longer, but needs to work on a single instance of a WorkbookPool.

I tried to split in the code below, but that does not work, because you cannot have @Shared Mocks, as I learned from this answer: https://stackoverflow.com/a/41194168/923041

Is there any way to avoid having a single, large test method with many when/then blocks?

@Stepwise
class WorkbookPoolSpec extends Specification {
    @Shared
    def workbook = Mock(Workbook)

    @Shared
    def workbookFactory = Mock(WorkbookFactory)

    @Shared
    def workbookPool = new WorkbookPool(workbookFactory)

    def 'workbook is created on check-out'() {
        when:
            workbookPool.borrowWorkbook()
        then:
            1 * workbookFactory.create() >> workbook
    }

    def 'workbook is reset on check-in'() {
        when:
            workbookPool.returnWorkbook(workbook)
        then:
            1 * workbook.reset()
    }
}
3
  • 1
    Could you please explain, why it must be shared? What happens if you remove all @Shared annotations? Sep 27, 2018 at 15:33
  • Yes, Stephen, mocks are cheap. Why share them? You should only share objects which are expensive (as in long execution time, high memory consumption, dependence on shared external resources such as databases) to create. You could just share the workbook pool if absolutely necessary, but use normal properties for the mocks.
    – kriegaex
    Sep 29, 2018 at 8:07
  • @LeonardBrünings: I just cannot create a new Mock of WorkbookPoolFactory without also creating a new WorkbookPool. However the WorkbookPool has state, for example in the small test extract that I posted, you cannot "return" a workbook that has not been "borrowed" before. So I need to user the same instance of WorkbookPool throughout the test.
    – eekboom
    Oct 1, 2018 at 7:45

2 Answers 2

2

You can use DetachedMockFactory together with @AutoAttach (new in 1.2).

@Stepwise
class WorkbookPoolSpec extends Specification {
  @AutoAttach
  def workbook = MockHolder.workbook

  @AutoAttach
  def workbookFactory = MockHolder.workbookFactory

  @Shared
  def workbookPool = new WorkbookPool(MockHolder.workbookFactory)

  def 'workbook is created on check-out'() {
    when:
    workbookPool.borrowWorkbook()
    then:
    1 * workbookFactory.create() >> workbook
  }

  def 'workbook is reset on check-in'() {
    when:
    workbookPool.returnWorkbook(workbook)
    then:
    1 * workbook.reset()
  }

  static class MockHolder {
    static Workbook workbook = new DetachedMockFactory().Mock(Workbook)
    static WorkbookFactory workbookFactory = new DetachedMockFactory().Mock(WorkbookFactory)
  }
}
1
  • Thanks! I did not know about that, yet. Very nice feature. In general I agree that mocks should not be shared, but I think in this case it makes sense.
    – eekboom
    Oct 1, 2018 at 12:04
1

In my understanding, the whole point of declaring a mock is to say: "My test execution verification criteria is that the actual invocations of methods on mock objects are getting verified" (otherwise there is no reason to use mocks).

But if so, there is no point in shared mocks, once a single test is done (verifications passed), the mock becomes irrelevant.

Now, the real question is why do you say: "but needs to work on a single instance of a WorkbookPool".

Maybe it worth to consider refactoring the code so that the creation of WorkbookPool (which BTW looks great because it has only one dependency) will be really an easy task so it will become a non-issue to create new mocks for every single test.

This way should answer the need for unit testing of methods exposed by the WorkbookPool object

If a single test should cover a flow end 2 end, then probably multiple when/then will be a better choice, although I personally don't use this kind of tests

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.