【问题标题】:only classes are allowed on the left hand side of a class literal when using Mockito and kotlin使用 Mockito 和 kotlin 时,只允许在类文字的左侧使用类
【发布时间】:2018-01-24 12:26:16
【问题描述】:

我使用Android studio的转换工具来转换现有的java测试类。

我收到此错误:

类文字的左侧只允许使用类

这是使用的测试用例:

Java

doAnswer(answerVoid(
            (OnDataListListener<List<BEntity>> myCallback) -> myCallback.onSuccess(mList))).when(
            mInteractor).performGetBList(any(OnDataListListener.class), anyBoolean());

科特林

doAnswer(answerVoid { listener: OnDataListListener<List<BEntity>> ->
      listener.onSuccess(
          emptyList())
    }).`when`<DragonInteractor>(mInteractor)
        .performGetBList(any<OnDataListListener>(OnDataListListener<*>::class.java),
            anyBoolean())

那么在这种情况下如何使用泛型参数呢?谢谢。

【问题讨论】:

    标签: android mockito kotlin


    【解决方案1】:

    只需使用OnDataListListener::class.java

    在类引用表达式中不需要(并且不允许)尖括号的原因是类引用与泛型完全无关,对于类的泛型特化没有不同的类引用。

    【讨论】:

    • 这里的“any”怎么样:any(OnDataListener::class.java)。我什至这里有这个但仍然没有编译:stackoverflow.com/a/32827711/2187976
    • @MohamedALOUANE,应该只是any(OnDataListListener::class.java)吗?如果编译不成功,试试any&lt;OnDataListListener&lt;*&gt;&gt;(OnDataListListener::class.java)
    【解决方案2】:

    先去掉OnDataListListener&lt;*&gt;::class.java参数 然后在OnDataListListener参数类型中添加List&lt;BEntity&gt;

    结果是

    doAnswer(answerVoid { listener: OnDataListListener<List<BEntity>> ->
      listener.onSuccess(
          emptyList())
    }).`when`<DragonInteractor>(mInteractor)
        .performGetBList(any<OnDataListListener<List<BEntity>>>(),
            anyBoolean())
    

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 1970-01-01
      • 2018-07-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      相关资源
      最近更新 更多