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

Type error when mocking java method with generic parameter declared without type parameter #86

Closed
simao opened this issue Dec 1, 2014 · 5 comments
Labels

Comments

@simao
Copy link

simao commented Dec 1, 2014

Hello,

I am using mock[InterfaceName] to mock a java interface, but I get the following error:

Error:(15, 23) kinds of the type arguments (java.util.Map,String,Unit) do not conform to the expected kinds of the type parameters (type T1,type T2,type R) in class MockFunction2.
java.util.Map's type parameters do not match type T1's expected parameters:
trait Map has two type parameters, but type T1 has none
  def epRuntime = mock[EPRuntime]
                      ^

I am trying to mock EPRuntime using:

import org.scalamock.scalatest.MockFactory
import org.scalatest.FunSuite

class BinlogToEsperSenderTest extends FunSuite with MockFactory {

Thank you

@pawel-wiejacha
Copy link
Collaborator

Here's the minimal example that fails to compile:

import java.util.Map;

public interface Issue86 {
  public void route(Map<Integer, Integer> map); // compiles
  public void route(Map map); // does not compile
}
val m = mock[Issue86]

paulbutcher added a commit that referenced this issue Feb 8, 2015
@pawel-wiejacha pawel-wiejacha changed the title Type error when mocking java interface Type error when mocking java method with generic parameter declared without type parameter Feb 18, 2015
@Lizzard28
Copy link

Is there a fix or is it not possible to mock something that has an unrefined Map, or any generic object without the generic explicitly defined

@barkhorn
Copy link
Collaborator

You can mock a raw type like this

Code:

import java.util.Map;

public interface Foo {
    public void route2(Map<String, Integer> map);
    public void route(Map map);
}

Test:

  it should "work" in  {
    trait FooB extends Foo {
      override def route(map: util.Map[_, _])
    }
    val m = mock[FooB]
  }

problem is the macro-generated mockfunction definition:
Failing:

val mock$route$0: MockFunction1[java.util.Map, Unit] = new MockFunction1[java.util.Map, Unit](JavaMocksTest.this._factory, scala.Symbol.apply(scala.Predef.augmentString("<%s> %s%s.%s%s").format($anon.this.mock$special$mockName, "Foo", "", "route", "")))
val mock$route2$0: MockFunction1[java.util.Map[String,Integer], Unit] = new MockFunction1[java.util.Map[String,Integer], Unit](JavaMocksTest.this._factory, scala.Symbol.apply(scala.Predef.augmentString("<%s> %s%s.%s%s").format($anon.this.mock$special$mockName, "Foo", "", "route2", "")))

With the overridden trait:

val mock$route$0: MockFunction1[java.util.Map[_, _], Unit] = new MockFunction1[java.util.Map[_, _], Unit](JavaMocksTest.this._factory, scala.Symbol.apply(scala.Predef.augmentString("<%s> %s%s.%s%s").format($anon.this.mock$special$mockName, "FooB", "", "route", "")))
val mock$route2$0: MockFunction1[java.util.Map[String,Integer], Unit] = new MockFunction1[java.util.Map[String,Integer], Unit](JavaMocksTest.this._factory, scala.Symbol.apply(scala.Predef.augmentString("<%s> %s%s.%s%s").format($anon.this.mock$special$mockName, "FooB", "", "route2", "")))

So a possible fix would be to identify missing type parameters in MockMaker.mockMethod, but for now there is solution where you can use a subtrait/interface.

@barkhorn barkhorn added this to the v3.6.0 milestone Jan 22, 2017
@barkhorn barkhorn removed the triage label Jan 22, 2017
@barkhorn
Copy link
Collaborator

barkhorn commented Mar 3, 2017

This is now mentioned in the FAQ: http://scalamock.org/user-guide/faq/

We will aim to make this easier to use without the additional boilerplate in a future version.

@goshacodes
Copy link
Collaborator

This methods has same type after erasure. Map[AnyRef, AnyRef]. This can't be fixed

Here's the minimal example that fails to compile:

import java.util.Map;

public interface Issue86 {
  public void route(Map<Integer, Integer> map); // compiles
  public void route(Map map); // does not compile
}
val m = mock[Issue86]

@goshacodes goshacodes closed this as not planned Won't fix, can't repro, duplicate, stale Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants