5

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 imported org.scalatest._ and org.scalamock.scalatest._. What else am I missing?

rabejens
  • 7,594
  • 11
  • 56
  • 104

3 Answers3

1

First of all, proxy mocks are not supported very well in ScalaMock 3, and I think they will be completely removed in ScalaMock 4. Do you really need to use proxy mocks instead macro mocks?

This should work:

package example

import org.scalatest.FlatSpec
import org.scalatest.Matchers
import org.scalamock.scalatest.proxy.MockFactory

trait MyInterface {
    def someMethod : String
}

class MyTest extends FlatSpec with Matchers with MockFactory {
  "MyInterface" should "work" in {
    val m = mock[MyInterface]
    m.expects('someMethod)().returning("foo")
    m.someMethod shouldBe "foo"
  }
}

If not, please check ScalaMock proxy mocks unit tests for more examples.

Pawel Wiejacha
  • 704
  • 7
  • 5
  • This example does not even find `mock`. I now stick with Mockito when needing such functionality. – rabejens Apr 27 '15 at 07:23
  • @rabejens it did not find the `mock` because `MyInterface` was not defined in my example. I've updated the example - it compiles and works with ScalaMock 3.2.x. Please note I'm mixing ScalaTest suite with `org.scalamock.scalatest.proxy.MockFactory` – Pawel Wiejacha Apr 27 '15 at 07:43
  • I try to mock a Java interface with this technique, and even when I import this and do `mock[MyInterface]`, it does not find `mock`. – rabejens Apr 27 '15 at 07:53
  • @rabejens please double check your test. I removed the `MyInterface` trait, created `src/main/java/example/MyInterface.java`, moved my test to `src/test/scala/example/MyTest.scala` and it works. I'm using scala-2.11.1 and ScalaMock-3.2.2. – Pawel Wiejacha Apr 27 '15 at 08:05
  • I double checked it. I even copy-pasted your code and it does not find `mock` there either. Maybe it is because I am building with Maven and miss some dependencies. Which dependencies did you specify for this to work? I currently have `org.scalatest:scalatest_2.11`, `org.scalamock:scalamock-core_2.11` and `org.scalamock:scalamock-scalatest-support_2.11`. – rabejens Apr 27 '15 at 09:39
  • `org.scalamock:scalamock-scalatest-support_2.11` should be enough. @rabejens I've just created a gist for you: https://gist.github.com/99b61345630a0beacd4d.git - it consists only of 3 files. You can clone it, move two files to correct folders (unfortunately, gists cannot have subfolders), and run `sbt test`. It will work. Then you experiment with minimal `pom.xml` to make this example work under maven. – Pawel Wiejacha Apr 27 '15 at 11:03
  • Your link just returns an empty page. – rabejens Apr 27 '15 at 15:21
  • use `git clone ` :) – Pawel Wiejacha Apr 27 '15 at 16:33
1

I think it should be something more like:

import org.scalamock.scalatest.MockFactory

class MyTest extends FlatSpec with Matchers with MockFactory {
  "MyInterface" should "work" in {
    val m = mock[MyInterface]
    (m.someMethod _).expects().returning("foo")
    m.someMethod shouldBe "foo"
  }
}

I think the expects arg is expecting the arg to the function

vinh
  • 11
  • 2
1

I use scalaMock version 4.1.0, this works for me:

For some trait:

trait MyInterface { def someMethod(n1: Int, n2: Int) }

This should be put into a test

val myInterfaceMock = mock[MyInterface]

myInterfaceMock.someMethod _ expects (1,2)

For more reading: scalaMock Guide, you'll find some examples there

Aaron_ab
  • 3,450
  • 3
  • 28
  • 42