Patterns for Saner Mocking

It is common to see mocks being setup this way in unit tests.

scenario("Test Case 1") {
    ...
    when(addressResolutionService.resolve(...)).thenReturn(...)
    when(vendorInventoryService.checkInventory(...)).thenReturn(...)
    ...
    .... another bunch of when and then returns
    when(shipmentService.schedule(...)).thenReturn(...)

    ...thisIsTheActualCalltoTest(...)

    verify(vendorInventoryService, 1).checkInventory(...)
    ... other such verifications
}

scenario("Test Case 2") {
    ...
    when(addressResolutionService.resolve(...)).thenReturn(...)
    when(vendorInventoryService.checkInventory(...)).thenReturn(...)
    ...
    .... another bunch of when and then returns ...give or take one or more mocks compared to the previous test ...
    when(shipmentService.schedule(...)).thenReturn(...)

    ...thisIsTheActualCalltoTest(...)

    verify(vendorInventoryService, 1).checkInventory(...)
    ... other such verifications
}

... other such test cases

Continue reading Patterns for Saner Mocking