Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use ScalaMock proxy mocks?

Tags:

scalatest

I have a very simple test and I'm trying to mock a trait. The test does not even run, and it fails with the initialization error: java.lang.IllegalArgumentException: requirement failed: Have you remembered to use withExpectations?

Here is my very simple test:

import org.scalatest._
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.matchers.ShouldMatchers
import org.scalamock.ProxyMockFactory
import org.scalamock.scalatest.MockFactory

@RunWith(classOf[JUnitRunner])
class TurtleSpec extends FunSpec with MockFactory with ProxyMockFactory {
  trait Turtle {
    def turn(angle: Double)
  }

  val m = mock[Turtle]
  m expects 'turn withArgs (10.0)

  describe("A turtle-tester") {
    it("should test the turtle") {
      m.turn(10.0)
    }
  }
}
like image 277
Mark Avatar asked Nov 12 '22 13:11

Mark


1 Answers

you need to call resetMocks / resetExpectations before running the test, the best way to do that is (ScalaTest way):

class TurtleSpec extends FunSpec with MockFactory with ProxyMockFactory with BeforeAndAfter {

  before {
    resetMocks()
    resetExpectations()
  }

  ...
}
like image 82
jdevelop Avatar answered Dec 20 '22 00:12

jdevelop