从 scala 调用重载的 java 泛型方法
我正在使用 Mockito 来模拟一个对象,该方法返回一个未参数化的 ArrayList,但我不知道如何让它工作
模拟的方法签名
public java.util.ArrayList getX()
测试代码
var mockee = mock(classOf[Mockee])
when(mockee.getX).thenReturn(Lists.newArrayList(x): ArrayList[_])
这实际上在 IntelliJ 中编译得很好,但在运行时会抛出:
[error] ....scala:89: overloaded method value thenReturn with alternatives:
[error] (java.util.ArrayList[?0],<repeated...>[java.util.ArrayList[?0]])org.mockito.stubbing.OngoingStubbing[java.util.ArrayList[?0]] <and>
[error] (java.util.ArrayList[?0])org.mockito.stubbing.OngoingStubbing[java.util.ArrayList[?0]]
[error] cannot be applied to (java.util.ArrayList[_$1])
[error] when(mockee.getX).thenReturn(Lists.newArrayList(x): ArrayList[_])
I'm using Mockito to mock an object with a method which returns an un-parametrized ArrayList, and I cannot figure out how to get this to work
Method signature to mock
public java.util.ArrayList getX()
Test code
var mockee = mock(classOf[Mockee])
when(mockee.getX).thenReturn(Lists.newArrayList(x): ArrayList[_])
This actually compiles fine in IntelliJ, but at runtime throws:
[error] ....scala:89: overloaded method value thenReturn with alternatives:
[error] (java.util.ArrayList[?0],<repeated...>[java.util.ArrayList[?0]])org.mockito.stubbing.OngoingStubbing[java.util.ArrayList[?0]] <and>
[error] (java.util.ArrayList[?0])org.mockito.stubbing.OngoingStubbing[java.util.ArrayList[?0]]
[error] cannot be applied to (java.util.ArrayList[_$1])
[error] when(mockee.getX).thenReturn(Lists.newArrayList(x): ArrayList[_])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下对我有用:
假设“Lists”类来自 Google 集合(现在是 Guava)。
The following works for me:
assuming the "Lists" class is from the Google collections (now Guava).