I have some Scala code that I wish to unit test using ScalaMock:
class CancellationManagerSpec extends FlatSpec with MockFactory { "Cancelling a shipment" should "check shipment is not already shipped" in { val manager = mock[CancellationsManager] (manager.checkStatus _).expects(*, *).returning(true).once() val request = CancellationRequestedEvent("shipmentNumber") manager.processCancellation(request) } }
The test fails:
[info] - should complete a series of tasks in happy case *** FAILED *** [info] Unexpected call: <mock-6> CancellationsManager.processCancellation(CancellationRequestedEvent(shipmentNumber)) [info] [info] Expected: [info] inAnyOrder { [info] <mock-6> CancellationsManager.checkStatus(*, *) once (never called - UNSATISFIED) [info] } [info] [info] Actual: [info] <mock-6> CancellationsManager.processCancellation(CancellationRequestedEvent(shipmentNumber)) (Option.scala:121)
I want to test that when I process a cancellation, certain tasks are done. More generally, there are logic such as this that I would like to test:
class SalesOrderShippedProcess { def execute(salesOrder: SalesOrder): Unit = { if (doTask1() && doTask2()) { doTask3() doTask4() doTask5() } } def doTask1(): Boolean = ??? def doTask2(): Boolean = ??? def doTask3(): Boolean = ??? def doTask4(): Boolean = ??? def doTask5(): Boolean = ??? }
Where for some process, I want to check task 3, 4, and 5 are done only if task 1 and 2 is successful, and even if task 3 fails, task 4 and 5 should execute regardless.
What’s the best way to test this? Is it failing because I’m calling a method of the mocked object directly? Do I need to move all the task methods to it’s own class then mock it, which seems a little strange to me just to be able to write test for it.