O'Reilly logo

Scala Test-Driven Development by Gaurav Sood

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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", ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required