使用Mockito& Guice在Scala中测试与泛型的接口 [英] Using Mockito & Guice to test interfaces with generics in Scala

查看:339
本文介绍了使用Mockito& Guice在Scala中测试与泛型的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个InputService特性方法

  def poll(参数:HashMap [String,String]):选项[T] 

其中T是通用的,所以InputService有一个类型参数[T]。



在我的模块中,我有

  val inputService:InputService [ String] = mock(classOf [InputService [String]])
bind [InputService [String]] .toInstance(inputService)

,在我的InputServiceTest中,我有

  var inputService:InputService [String] = _ 
之前{
inputService = Guice.createInjector(new MockWatcherModule).getInstance(classOf [InputService [String]])
}

但问题是当我运行它时,它给了我这个错误

 调用嵌套套件运行时遇到异常 -  Guice配置错误:
1)没有为services.InputService实现绑定。
同时定位services.InputService

我认为这是因为它正在寻找services.InputService绑定,但它只有services.InputService [String]。但是,当我只使用InputService而不是InputService [String]时,我得到错误 Trait缺少类型参数



任何建议?



编辑:
原来我可以使用scala-guice和KeyExtensions中的typeLiteral来解决我的问题。感谢Tavian!

解决方案

由于类型擦除,在 getInstance(classOf [InputService [String]] )调用,你只是传递 InputService.class 。您需要传递 TypeLiteral 来编码通用类型信息。从快速Google看起来像

  import net.codingwell.scalaguice._ 
import net.codingwell.scalaguice.InjectorExtensions._

Guice.createInjector(new MockWatcherModule).instance [InputService [String]]

会起作用。


I am new to Scala, and I'm running into this problem when I'm trying to unit test some of my interfaces.

I have an InputService trait with method

def poll(parameters: HashMap[String, String]): Option[T]

where T is generic, so InputService has a type parameter [T].

In my module, I have

val inputService: InputService[String] = mock(classOf[InputService[String]])
bind[InputService[String]].toInstance(inputService)

and in my InputServiceTest, I have

  var inputService: InputService[String] = _
  before {
    inputService = Guice.createInjector(new MockWatcherModule).getInstance(classOf[InputService[String]])
  }

But the issue is when I run it, it gives me this error

Exception encountered when invoking run on a nested suite - Guice configuration errors:
1) No implementation for services.InputService was bound.
  while locating services.InputService

I think it's because it's looking for services.InputService to bound, but it only has services.InputService[String]. However, when I just use InputService instead of InputService[String], I get the error Trait missing Type Parameter.

Any suggestions?

EDIT: Turns out that I can use typeLiteral from scala-guice and KeyExtensions to solve my issue. Thanks Tavian!

解决方案

Due to type erasure, in the getInstance(classOf[InputService[String]]) call, you're just passing InputService.class. You need to pass a TypeLiteral instead to encode the generic type information. From a quick Google it looks like

import net.codingwell.scalaguice._
import net.codingwell.scalaguice.InjectorExtensions._

Guice.createInjector(new MockWatcherModule).instance[InputService[String]]

will work.

这篇关于使用Mockito& Guice在Scala中测试与泛型的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆