Questions tagged [scalamock]

ScalaMock is a mocking framework for the Scala programming language.

ScalaMock provides fully type-safe mocking support for almost all Scala language features including:

  • mocking classes, traits and case classes
  • mocking functions and operators
  • mocking type parameterized and overloaded methods
  • support for type constraints
  • support for repeated parameters and named parameters
  • mocking Java classes and interfaces

The official Web site for the ScalaMock project is http://www.scalamock.org.

148 questions
4
votes
1 answer

Scala mock, MockFactory expects fails with 'Unexpected call'

I'm trying to follow this doc - scalatest with scala-mock to mock the function and check if it has been called class AggregateSpec extends FlatSpec with Matchers with MockFactory { val data = Seq("This", "is", "something", "I", "would", "like",…
ses
  • 13,174
  • 31
  • 123
  • 226
4
votes
2 answers

Mocking in scala generates a java.lang.NoSuchMethodException

Hey I am trying to test the following class : class Foo { def f: Int = 4 + g def g: Int = 2 } My test is the following: class FooSpec extends PlaySpec with MockFactory { val foo = new Foo() "Foo" must { "Call function f" in { …
Scipion
  • 11,449
  • 19
  • 74
  • 139
4
votes
0 answers

Scalamock in asynchronous context

I would like to mock some service that i pass to an actor. Then i want to verify that the mocked service is called as it should. Preferably i want to pass a stub, so i can simulate a service behavior for the sake of the test. However i don't know…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
4
votes
2 answers

scalamock and method with multiple argument

i am trying to mock an object that has a function with multiple argument. I would just try to set the expectation for it. That is, somehting of the form: (item.addMetadata(,,,,,)).expects("","","","","","","") I just don't know how to write it. The…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
4
votes
1 answer

scalamock: Wildcard argument match on subtype

In my class, I have two versions of a method. One takes an Exception, the other a String. class Foo { def method(e: Exception) = ??? def method(s: String) = ??? } In JMock, I can mock a call to a method based on its type. Notice, I'm using a…
Toby
  • 9,523
  • 8
  • 36
  • 59
4
votes
1 answer

Mocking a val of a trait with scala-mock

I'd like to mock a val of a trait. for example, in this code, to mock the val baz: trait Foo { def bar(): Int val baz: Int } val fooMock = mock[Foo] (fooMock.bar _).expects().returning(5) (fooMock.baz _).expects().returning(6) //doesn't…
lev
  • 3,986
  • 4
  • 33
  • 46
3
votes
2 answers

Do we need to test a failure scenario of a public method which fails with an Exception?

I have a case class Employee defined as case class Employee(.........fields.....) I have a method say def getEmployees(organization: String): Future[Seq[Employee]] = { val result = employeeClient.getAllEmployees(organization) // some logic…
user9920500
  • 606
  • 7
  • 21
3
votes
1 answer

How do you create a ScalaMock stub that doesn't call the constructor of the underlying object?

Consider the following example Scala class and unit test: class BrokenClass(s: String) { private val len = s.length def length(): Int = len } class BrokenTest extends FlatSpec with Matchers with MockFactory { "A BrokenClass" should "stub…
Huitzilopochtli
  • 191
  • 2
  • 13
3
votes
1 answer

ScalaMock, Unexpected call: when sharing mock instance between tests

I am using Scala 2.10 with ScalaMock 3.6. I have a quite simple test case with 4 test scenarios. I have created a mock object for those tests to use (imitating file system): class ProcessingOperatorTest extends FlatSpec with Matchers with…
Atais
  • 10,857
  • 6
  • 71
  • 111
3
votes
2 answers

ScalaMock: Can't handle methods with more than 22 parameters (yet)

Scalamock reject my mocking attempt by saying that it doesnt support yet more than 22 methods. The reason is because there are more than 22 methods, all together, in the class I am trying to mock (2 are mine, 20+ are mixed in (from Akka Json…
David Puleri
  • 144
  • 8
3
votes
2 answers

How to set up a ScalaMock once for repeated method invocations?

I want to set up a test in Scala that creates a mock config to provide certain values. I'm using ScalaTest 3.0.1, ScalaMock 3.4.2, and typesafe 1.3.1. The goal is to mock values for the config before running tests. The docs…
Don Branson
  • 13,631
  • 10
  • 59
  • 101
3
votes
3 answers

ScalaMock with scalatest

I am new to scalatest and scalamock. Here is what I have it in my sbt file name := "cakepattern" version := "0.1" scalaVersion := "2.11.8" libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % "3.0.0" % "test", "org.scalamock" %%…
Abdul Rahman
  • 1,294
  • 22
  • 41
3
votes
0 answers

How to test overload function with repeated parameters using scalamock?

trait Operator{ def addRole(index:Int,roles:String*) def addRole(attrIndexes:Set[Int],role:String) } How to test the first addRole using scalaMock? I have tried ,all doesn't work. (fakeContext.addRole(_:Int,_:String))…
kali
  • 31
  • 3
3
votes
2 answers

Why don't specs2 + scalamock tests run in IntelliJ? Multiple suite traits detected

I'm trying to run Specs2 tests from IDEA from both Windows and Mac versions of Intellij IDEA 14. I generated the idea project files using both gen-idea and the built in SBT plugin and get the same results... When I try to run them, I get Error…
Toby
  • 9,523
  • 8
  • 36
  • 59
3
votes
1 answer

mocking methods which use ClassTag in scala using scalamock

My first question, question, was answered but it uncovered another issue I am having. Here is the scenario. Example code (expanded from previous question) A Model: case class User (first: String, last: String, enabled: Boolean) Component…
IUnknown
  • 2,596
  • 4
  • 35
  • 52
1
2
3
9 10