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
29
votes
3 answers

Scalamock 3. Mock overloaded method without parameter

I couldn't find any documentation that explains how to mock overloaded methods that take no arguments in scalamock e.g public boolean isInfoEnabled(Marker marker); public boolean isInfoEnabled(); To mock the function that takes the Marker, one can…
user2715478
  • 1,273
  • 12
  • 17
13
votes
2 answers

ScalaMock: How to mock/stub a method to return different values per call?

Using ScalaMock, I want to mock/stub a class method so it will return a different value per call (order of calls matters). I can achieve this with mock and expects, but that will force me to verify those calls. Can I do this with a stub? Also, how…
Eyal Roth
  • 3,895
  • 6
  • 34
  • 45
12
votes
3 answers

Scala Mock partial stubbing

I'd like to stub one of the methods of a scala class with dependencies. Is there a way to achieve this using ScalaMock? Here is a simplified example of what I have: class TeamService(val dep1: D1) { def method1(param: Int) = param *…
Danix
  • 1,947
  • 1
  • 13
  • 18
10
votes
1 answer

Scalatest ExecutionContext

I would like to know which ExecutionContext I should use (and why) on scalatest % 2.2.6 to run my futures and mock's futures. class Foo { def foo: Future[String] = Future.sucessful("B") } class Bar(foo: Foo) { def bar: Future[String] =…
Henrique Goulart
  • 1,815
  • 2
  • 22
  • 32
6
votes
2 answers

Is it possible to mock / override dependencies / imports in Scala?

I have some code looking like this: package org.samidarko.actors import org.samidarko.helpers.Lib class Monitoring extends Actor { override def receive: Receive = { case Tick => Lib.sendNotification() } } Is there a way to…
samidarko
  • 590
  • 7
  • 20
6
votes
1 answer

How to mock a class with ScalaMock

In the doc of ScalaMock, it is said that: mocking classes, traits and case classes is one of the feature supported. I have the following case class: case class Thing(private val item: Item) When I do: val item = mock[Thing] I get the following…
MaatDeamon
  • 9,532
  • 9
  • 60
  • 127
6
votes
1 answer

ScalaMock mocking a trait gives "MockFunction1 cannot be cast to StubFunction1"

The following code: import org.scalamock.scalatest.MockFactory import org.scalatest.FlatSpec trait SomeTrait { def getLongByInt(int: Int): Long } class TestScalaMock extends FlatSpec with MockFactory { "Scala Mock" should "mock my trait" in…
simbo1905
  • 6,321
  • 5
  • 58
  • 86
5
votes
1 answer

Scala Mock Syntax (class _).expects meaning?

New to Scala, have searched far and wide for clarification on some ScalaMock syntax. As per this guide, I keep seeing the following general testing pattern: (myClass.myMethod _).expects() What exactly is happening here? What function does the…
Roy Taylor
  • 63
  • 4
5
votes
2 answers

Mocking SparkSession for unit testing

I have a method in my spark application that loads the data from a MySQL database. the method looks something like this. trait DataManager { val session: SparkSession def loadFromDatabase(input: Input): DataFrame = { …
rogue-one
  • 11,259
  • 7
  • 53
  • 75
5
votes
1 answer

scalamock mocking java interface method varargs

I need to mock a method with varargs from a java interface public interface MyClient { MyResponse indexPrivileges(IndexPrivilege... parameters); } and I am mocking it with (mockMyClient.indexPrivileges _).expects(*).returns(response) but get…
rojanu
  • 1,592
  • 5
  • 20
  • 34
5
votes
2 answers

Mock a method without arguments but with implicit parameters

abstract trait MyApi { def getResult()(implicit ec: ExecutionContext): Future[String] } The following doesn't work: val m = mock[MyApi] (m.getResult _).expects() returning "..." It fails with: java.lang.ClassCastException:…
douglaz
  • 1,306
  • 2
  • 13
  • 17
5
votes
3 answers

Scalamock: How to get "expects" for Proxy mocks?

I am using Scalamock with ScalaTest, and am trying to mock a Java interface. I currently have: private val _iface = mock [MyInterface] now I want to do _iface expects `someMethod returning "foo" once But the compiler does not find expects. I…
rabejens
  • 7,594
  • 11
  • 56
  • 104
4
votes
1 answer

Mock partially a class with scalamock

I'm trying to test a class Cls with two functions: A and B. A loads a DataFrame and B calls A then does some operations and returns a new DataFrame. For the sake of example: class Cls { def A(dummy: Int): Int = 5 def B(): Int = A(7) +…
MrMaxPayne
  • 173
  • 5
4
votes
1 answer

Scalamock: Mocking a generic case class results in type mismatch

I'm using Mongodb as persistence in my application and I'm currently writing test for my code. My CUT looks as following implicit def storageHandler[M[_]: Monad]( implicit mongoDatabase: MongoDatabase ) = new Storage.Handler[M] { override…
Mauro Palsgraaf
  • 237
  • 3
  • 13
4
votes
1 answer

How do you use scalamock to mock a class with constructor parameters

I know how to mock a class that has no constructor parameters e.g., myMock = mock[MockClass] However, what do you do if the class has constructor parameters? More specifically I'm trying to mock the finatra class:…
ThinkBonobo
  • 15,487
  • 9
  • 65
  • 80
1
2 3
9 10