• Stars
    star
    497
  • Rank 85,149 (Top 2 %)
  • Language
    Scala
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Native Scala mocking framework

ScalaMock

Native Scala mocking.

Official website: scalamock.org

Examples

Expectations-First Style

test("drawline interaction with turtle") {
  // Create mock Turtle object
  val m = mock[Turtle]
  
  // Set expectations
  (m.setPosition _).expects(10.0, 10.0)
  (m.forward _).expects(5.0)
  (m.getPosition _).expects().returning(15.0, 10.0)

  // Exercise System Under Test
  drawLine(m, (10.0, 10.0), (15.0, 10.0))
}

Record-then-Verify (Mockito) Style

test("drawline interaction with turtle") {
  // Create stub Turtle
  val m = stub[Turtle]
  
  // Setup return values
  (m.getPosition _).when().returns(15.0, 10.0)

  // Exercise System Under Test
  drawLine(m, (10.0, 10.0), (15.0, 10.0))

  // Verify expectations met
  (m.setPosition _).verify(10.0, 10.0)
  (m.forward _).verify(5.0)
}

A more complete example is on our Quickstart page.

Features

  • Fully typesafe
  • Full support for Scala features such as:
    • Polymorphic (type parameterised) methods
    • Operators (methods with symbolic names)
    • Overloaded methods
    • Type constraints
  • ScalaTest and Specs2 integration
  • Mock and Stub support
  • Macro Mocks and JVM Proxy Mocks
  • Scala.js support
  • built for Scala 2.12, 2.13, 3
  • Scala 2.10 support was included up to ScalaMock 4.2.0
  • Scala 2.11 support was included up to ScalaMock 5.2.0

Using ScalaMock

Artefacts are published to Maven Central and Sonatype OSS.

For ScalaTest, to use ScalaMock in your Tests, add the following to your build.sbt:

libraryDependencies += Seq("org.scalamock" %% "scalamock" % "5.2.0" % Test,
    "org.scalatest" %% "scalatest" % "3.2.0" % Test)

Scala 3 Migration Notes

  1. Type should be specified for methods with by-name parameters
trait TestTrait:
  def byNameParam(x: => Int): String

val t = mock[TestTrait]

// this one no longer compiles
(t.byNameParam _).expects(*).returns("")

// this one should be used instead
(t.byNameParam(_: Int)).expects(*).returns("")
  • Not initialized vars are not supported anymore, use scala.compiletime.uninitialized instead
  • Vars are not mockable anymore
trait X:
  var y: Int  // No longer compiles
  
  var y: Int = scala.compile.uninitialized // Should be used instead

Mocking of non-abstract java classes is not available without workaround

public class JavaClass {
    public int simpleMethod(String b) { return 4; }
}
val m = mock[JavaClass] // No longer compiles

class JavaClassExtended extends JavaClass

val mm = mock[JavaClassExtended] // should be used instead

Documentation

For usage in Maven or Gradle, integration with Specs2, and more example examples see the User Guide

Acknowledgements

YourKit is kindly supporting open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of innovative and intelligent tools for profiling Java and .NET applications. Take a look at YourKit's leading software products: YourKit Java Profiler and YourKit .NET Profiler.

Many thanks to Jetbrains for providing us with an OSS licence for their fine development tools such as IntelliJ IDEA.

Also, thanks to https://github.com/fthomas/scala-steward for helping to keep our dependencies updated automatically.

More Repositories

1

borachio

Native Scala mocking framework
Scala
33
star
2

electron-app

Electron app with deps.edn, figwheel.main, reagent, and test integration
Clojure
29
star
3

lein-lambda

A Leiningen plugin to automate AWS Lambda deployments
Clojure
28
star
4

foldable-seq

Making lazy sequences foldable
Clojure
17
star
5

mockito-on-android

Sample demonstrating Mockto on Android
Java
9
star
6

AkkaProducerConsumerBenchmarks

Scala
8
star
7

clj-migratus

Clojure
6
star
8

ScalaMockExample

Example of using ScalaMock
Scala
5
star
9

smock

Native Scala mocking framework
Scala
5
star
10

foldable-seq-example

An example of using foldable-seq
Clojure
3
star
11

ScalaKey

Scala
3
star
12

parallel-word-count

Parallel word counting in Clojure
Clojure
2
star
13

chess-scala

Scala
2
star
14

mockturtle

Borachio sample
Scala
2
star
15

scalamock-sbt-plugin

Sbt plugin for ScalaMock
Scala
2
star
16

scala-macro-experiments

Scala
2
star
17

powermocking

Sample code for "Power mocking in Scala with Borachio"
Scala
2
star
18

Bags

For Sam
Scala
2
star
19

borachio-sbt-plugin

Plugin for sbt 0.11 for compiling with Borachio
Scala
2
star
20

powercontrol

A demonstration of mocking Android components with Borachio
Java
1
star
21

typesafemocks

An example of the experimental typesafe mock support in Borachio
Scala
1
star
22

warehousemanager

Sample demonstrating Borachio on Android
Java
1
star
23

baderrormessage

Scala
1
star
24

docproblem

Scala
1
star
25

implementor

Scala
1
star
26

sbtproblem

Scala
1
star
27

chess-clojure

Clojure
1
star
28

androidistesthostile

Sample demonstrating some of the challenges of testing on Android
Java
1
star
29

datalog-flix

Code to accompany An Introduction to Datalog in Flix
1
star
30

scala-macro-bug

Scala
1
star
31

MrsDoyle

Scala
1
star
32

scalargs

Scala
1
star
33

paulbutcher.github.io

JavaScript
1
star
34

mylocation

Scala
1
star