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()
}
}
@Shared
annotations?