Mockito

MockitoSugar in ScalaTest provides syntactic sugar for using Mockito with ScalaTest.

In my experience, I have found Mockito to be the easiest to use among all the frameworks. It is quite easy to create a mock from ServiceClass by simply using mock[ServiceClass].

Mockito leads to a very understandable test format. I think that is one of the reasons for its popularity over jMock and EasyMock.

Let us rewrite the same example from jMock and EasyMock with Mockito. It will look something like this:

// First, create the mock object val mockService = mock[ServiceClass] // Create the class under test and pass the mock to it classUnderTest = new ClassUnderTest classUnderTest.addService(mock) // Use the class under test classUnderTest.createPayroll("Person1", ...

Get Scala Test-Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.