我有一个 Guice Module
提供 List<String>
使用 @Provides
- 带注释的方法。
class TestModule() : Module {
override fun configure(binder: Binder) {}
@Provides fun getStrings(): List<String> = listOf("foo", "bar")
}
class Test {
@Test fun `provider can not deliver`() {
val injector = Guice.createInjector(TestModule())
injector.getInstance(object : Key<List<String>>() {})
}
}
但是,测试失败了:
1) No implementation for java.util.List<? extends java.lang.String> was bound.
while locating java.util.List<? extends java.lang.String>
现在,这似乎与 this question 相同但我不知道在哪里添加 @JvmSuppressWildcards
注解;将其添加到 getStrings()
方法不会改变任何东西,就像将它添加到 object
在 getInstance()
称呼。如何让 Guice 做我想做的事?
最佳答案
经过多次反复试验,我得出了这个解决方案:
@Test
fun `provider can not deliver`() {
val injector = Guice.createInjector(TestModule())
injector.getInstance(object : Key<List<@JvmSuppressWildcards String>>() {})
}
这是我认为注释实际上有效但突然测试变为绿色的最后一个地方。
关于guice - 在 Kotlin & Guice 中提供一个通用实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42038982/