Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to mock Java Generic interface with overloaded method (different number of params) #225

Open
NoamShaish opened this issue Apr 26, 2018 · 2 comments

Comments

@NoamShaish
Copy link

ScalaMock Version

3.6.0

Scala Version

2.11

Runtime

JVM

Please describe the expected behavior of the issue

I would expect to mock Java Generic interfaces having overloaded method with different number of params in the same way I mock other Java interfaces or Scala traits

Please provide a description of what actually happens

When trying to mock this interface I get the following compile error:

 error: value expects is not a member of (String, java.util.concurrent.Callable[String]) => Unit
[ERROR]     (m.send(_: String, _: Callable[String])).expects(*, *)
[ERROR]                                              ^

 error: value expects is not a member of String => Unit
[ERROR]     (m.send(_: String)).expects(*).once
[ERROR]                         ^

Reproducible Test Case

import java.util.concurrent.Callable;

public interface GOInterface<T> {
    void send(T record);
    void send(T record, Callable<T> onComplete);
}
it should "mock java generic interface with overloaded method (with different number of parameters)" in {
    import java.util.concurrent.Callable
    var result = ""
    val m = mock[GOInterface[String]]
    (m.send(_: String, _: Callable[String])).expects(*, *)
      .onCall{ case(s: String, c: Callable[String]) => c.call()}.once

    m.send("hello", new Callable[String] {
      override def call(): String = {result = "world"; result}
    })

    result should be("world")
  }

it should "mock java generic interface with overloaded method (with different number of parameters) 2" in {
    import java.util.concurrent.Callable
    var result = ""
    val m = mock[GOInterface[String]]
    (m.send(_: String)).expects(*).once

    m.send("hello")

    result should be("")
  }
@barkhorn
Copy link
Collaborator

barkhorn commented May 6, 2018

We have several tests related to overloaded methods, have a look here:

it should "mock a Java class with an overloaded method (different param count)" in { // test for issue #34
val m = mock[JavaClassWithOverloadedMethod]
(m.overloadedMethod(_: String)).expects("a").returning("first")
(m.overloadedMethod(_: String, _: String)).expects("a", "b").returning("second")
m.overloadedMethod("a") shouldBe "first"
m.overloadedMethod("a", "b") shouldBe "second"
}
it should "mock a Java class with an overloaded method (the same param count)" in { // test for issue #73
val m = mock[JavaClassWithOverloadedMethod]
(m.overloadedSameParamCount(_: String)).expects("one").returning("first")
(m.overloadedSameParamCount(_: Integer)).expects(new Integer(2)).returning(2)
m.overloadedSameParamCount("one") shouldBe "first"
m.overloadedSameParamCount(2) shouldBe 2
}
it should "mock a Java class with an overloaded method (with primitive param)" in { // test for issue #73
val m = mock[JavaClassWithOverloadedMethod]
(m.overloadedWithPrimitiveParam(_: String)).expects("one").returning("first")
(m.overloadedWithPrimitiveParam(_: Int)).expects(2).returning("second")
m.overloadedWithPrimitiveParam("one") shouldBe "first"
m.overloadedWithPrimitiveParam(2) shouldBe "second"
}
it should "mock a Java class with an overloaded method (with type params)" in {
val m = mock[JavaClassWithOverloadedMethod]
(m.overloadedGeneric(_: String)).expects("one").returning("first")
(m.overloadedGeneric(_: Int)).expects(2).returning("second")
m.overloadedGeneric("one") shouldBe "first"
m.overloadedGeneric(2) shouldBe "second"
}

what if you declare trait StringGoInterface extends GoInterface[String] and create a mock[StringGoInterface] instead. That should work

@NoamShaish
Copy link
Author

I looked into the provided tests. even in one of the forks which have more tests. But my edge case isnt covered.

I tried you approach with a trait but I got the same result.
The only solution I found was to create a dummy class implementing the interface:

class JavaInterfaceTest extends FlatSpec with Matchers with MockFactory {
  behavior of "scalamock"

  class StringInterface extends GOInterface[String] {
    override def send(record: String): Unit = ()

    override def send(record: String, onComplete: Callable[String]): Unit = ()
  }

  val call: (String, Callable[String]) => Unit = { case(s: String, c: Callable[String]) => c.call()}
  
  it should "mock java generic interface with overloaded method (with different number of parameters)" in {
    var result = ""
    val m = mock[StringInterface]
    (m.send(_: String, _: Callable[String])).expects(*, *)
      .onCall{ call }.once

    m.send("hello", new Callable[String] {
      override def call(): String = {result = "world"; result}
    })

    result should be("world")
  }


  it should "mock java generic interface with overloaded method (with different number of parameters) 2" in {
    var result = ""
    val m = mock[StringInterface]
    (m.send(_: String)).expects(*).once

    m.send("hello")

    result should be("")
  }
}

for this example its quit simple but for more complicated interfaces its might be hard/ugly to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants