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
2
votes
0 answers

ScalaMock: Is it possible to mock a trait with an overloaded method expecting a curried function parameter?

My application is using Scala 2.13 and ScalaMock 5.1. Is it possible to mock the bar method in a trait like this one? trait Foo { def bar(f: Double): Double def bar(i: Int)(f: => Double): Double } The following attempt does not compile. The…
Daryl Odnert
  • 522
  • 3
  • 15
2
votes
1 answer

How to mock/stub only one field in a case class

I have seen many articles on how to mock the method in the case class when testing with scalamock. However, sometimes I need to mock only one field. For example when I'm testing a very narrow workflow. I thought that stub[Type].copy(f = "1") would…
Konstantin Bodnia
  • 1,372
  • 3
  • 20
  • 43
2
votes
1 answer

How to mock a method without a parameter list with ScalaMock 5.0.0?

When trying to mock a method declared as def foo: Int with val mock = mock[MyClass] (mock.foo _).expects().returning(10) I get an error Error: Methods without a parameter list and by-name params can no longer be converted to functions as `m _`,…
Ava
  • 818
  • 10
  • 18
2
votes
3 answers

How to call real method on a stub

Is there a way to call a real method on a stubbed object with scalamock? I would like to be able to do something like this: class MySpec extends FunSpec with Matchers with MockFactory { trait MyTrait { def f1: Int def f2: Int = f1 } …
WilQu
  • 7,131
  • 6
  • 30
  • 38
2
votes
2 answers

Mocking configuration objects with MockFactory

I am doing some tests, and in many cases I have a configuration of an FTP / HTTP. I am working with Scala and the following libraries in my sbt: "org.scalatest" %% "scalatest" % "3.0.1" % Test, "org.scalamock" %% "scalamock" % "4.1.0" % Test, I am…
juan garcia
  • 1,326
  • 2
  • 23
  • 56
2
votes
1 answer

Using scalamock: Could not find implicit value for evidence parameter of type error

I am writing unit tests for my spark/scala application. I am using scalamock as well to mock objects, specifically Session / Session Factory. In one of my test classes, I try to mock the Session. Ex: val mockedSession = mock[Session] However, I get…
Kevin Zhou
  • 85
  • 1
  • 6
2
votes
4 answers

Cannot mock WSRequest.post() using scalamock

I am writing unit tests for Play application using Scalamock and Scalatest. My original code looks like: // Here ws is an injected WSClient val req = Json.toJson(someRequestObject) val resp: Future[WSResponse] =…
Xolve
  • 22,298
  • 21
  • 77
  • 125
2
votes
1 answer

Scala mock function with implicit generic types

I'm trying to mock Cassandra ScalaGettableData object using scalamock. I need to mock the following method: def getMap[K : TypeConverter, V : TypeConverter](name: String) = get[Map[K, V]](name) TypeConverter is a Trait and has implicit…
Gridou
  • 109
  • 7
2
votes
1 answer

ScalaMock class vals

class MyClassWithVals(val a: String, val b: String, val c: String) {} Can I mock this with ScalaMock in some such that: val mock = stub[MyClassWithVals] //when mock.a expect "The value of a" The reason I want to mock this, rather than just…
David B
  • 455
  • 6
  • 13
2
votes
1 answer

Unit testing class that extends a trait - how do I mock and stub methods in the trait?

I'm using scalatest to unit test a class that extends a trait (in the sense that my class is using the trait as a mixin). The trait contains methods that are helper methods (which ultimately call a few lookups in a database) which I would like to…
moncheery
  • 333
  • 4
  • 17
2
votes
1 answer

In ScalaMock, how to return a mockClass whenever someone new an instance of a class?

In PowerMockito, we can use the pattern "whenNew(MyClass).thenReturn(mockMyClass)" when someone wants to new an instance of MyClass, it will receive mockMyClass instead of the real instance. Can we do similar things in ScalaMock or EasyMock? I…
Marco Dinh
  • 332
  • 2
  • 15
2
votes
1 answer

Does ScalaMock support mocking of (companion) objects and constructors?

There are somewhat contradictory statements about the abilities of ScalaMock to mock (companion) objects and constructors. The page ScalaMock step-by-step states it can also mock: Classes *Singleton and companion objects (static methods) Object…
deamon
  • 89,107
  • 111
  • 320
  • 448
2
votes
2 answers

Scala Testing: Replace function implementation

Using ScalaTest, I want to replace a function implementation in a test case. My use case: object Module { private def currentYear() = DateTime.now().year.get def doSomething(): Unit = { val year = currentYear() // do something with the…
Heinzi
  • 5,793
  • 4
  • 40
  • 69
1
vote
1 answer

Mock a function call in scala for unittesting

I would like to write a unittest to the function import com.github.nscala_time.time.Imports.{DateTime, richReadableInstant} def myFunction(ts: Long):Long = { (new DateTime(ts) to DateTime.now()).toDurationMillis } Is it possible to somehow mock…
bayerb
  • 649
  • 2
  • 9
  • 28
1
vote
0 answers

ScalaMock: Can't log call to mock object, have expectations been verified already

Im running into an issue with scala mock. Where the error message indicates that it cannot log a call to a mock object. I've set up the following spec. I've a few mocks setup but nothing to extraordinary: "get accounts" in { import…
1 2
3
9 10