EasyMock. Add EasyMock dependency to your POM file.
<dependency><groupId>org.easymock</groupId><artifactId>easymock</artifactId><version>3.4</version><scope>test</scope></dependency> Then, you need a class to test and a collaborator used by this class. The collaborator can be a class or an interface. It doesn't matter. Please don't create an interface just for the pleasure of mocking it. public interface Collaborator { void documentAdded(String title);} public class ClassUnderTest { private Collaborator listener; // ... public void setListener(Collaborator listener) { this.listener = listener; } public void addDocument(String title, byte[] document) { // ... }} In our case, we are expecting the collaborator to be notified when a document is added to the class under test.
Create the mock Have it set to the tested class Record what we expect the mock to do Tell all mocks we are now doing the actual testing Test Make sure everything that was supposed to be called was called Then the code fulfilling it: GitHub - paulbutcher/ScalaMock: Native Scala mocking framework. Download ScalaCheck. Here you can download the current release of ScalaCheck manually, or get instructions on how to use sbt or Maven to retrieve ScalaCheck automatically as part of your build process.
ScalaCheck has no dependencies other than the Scala runtime. If you need help setting it up, take a look at the Scala documentation. Since the various major releases of the Scala compiler aren’t compatible with each other, ScalaCheck is built for each major version of Scala. Below you can find ScalaCheck builds available for download. Make sure you download the build that matches the version of the Scala compiler you’re using. ScalaCheck 1.12.5 builds Sources and API documentation sbt If you’re using sbt it is very easy to include ScalaCheck as a dependency in your project, since sbt automatically selects and downloads the correct build of ScalaCheck for the version of Scala you’re using. LibraryDependencies += "org.scalacheck" %% "scalacheck" % "1.12.5" % "test"
ScalaCheck. ScalaMock - Home. ScalaCheck. Specs2.org. Specify your software using both text and Scala code class HelloWorldSpec extends Specification { def is = s2""" This is a specification for the 'Hello world' string The 'Hello world' string should contain 11 characters $e1 start with 'Hello' $e2 end with 'world' $e3 """ def e1 = "Hello world" must haveSize(11) def e2 = "Hello world" must startWith("Hello") def e3 = "Hello world" must endWith("world") } Use different styles of specifications /** This is the "Unit" style for specifications */class HelloWorldSpec extends Specification { "This is a specification for the 'Hello world' string".txt "The 'Hello world' string should" >> { "contain 11 characters" >> { "Hello world" must haveSize(11) } "start with 'Hello'" >> { "Hello world" must startWith("Hello") } "end with 'world'" >> { "Hello world" must endWith("world") } }} Document your APIs with compiler-checked examples Manage contexts for integration testing.
Gilt ♥ Open Source / Which Scala Testing Tools Should You Use? By Gilt Senior Software Engineer Lukasz Szwed “Scala testing systems have also stepped out on their own and created some of the most mind-blowing testing tools found in any language.”
–Daniel Hinojosa, author of Testing in Scala At Tuesday’s Dublin Scala Users Group meetup, organized by Gilt, I presented a tech talk on standard test-driven development practices available to Scala developers. These include ScalaTest, specs2, ScalaCheck, and various mocking frameworks. The event was great: we kicked things off with wood-fired pizza and a very good selection of beers. I started things off by introducing ScalaTest and specs2, which share a few things in common: About that last point: Cucumber is written in and for Ruby. Writing tests in or specs2 can help you turn old-but-good JUnit/TestNG code into actively used assets and eliminate the need for documentation.
ScalaTest.