java与Kotlin的等价物是什么;s函数类型?
我在java与Kotlin的等价物是什么;s函数类型?,java,mocking,mockito,kotlin,Java,Mocking,Mockito,Kotlin,我在Kotlin中有代码,在Java中有测试代码。由于Kotlin和Mockito不是最好的朋友,所以我没有将测试代码迁移到Kotlin 在Kotlin中,我有块类型的方法。例如: open fun getProductInfo(resultListener: (List<Deal>)->Unit, errorListener: (Throwable)->Unit) { ... } 为什么不在Android Studio或IntelliJ中尝试从Kotlin获取
Kotlin
中有代码,在Java
中有测试代码。由于Kotlin
和Mockito
不是最好的朋友,所以我没有将测试代码迁移到Kotlin
在Kotlin
中,我有块类型的方法。例如:
open fun getProductInfo(resultListener: (List<Deal>)->Unit, errorListener: (Throwable)->Unit)
{
...
}
为什么不在Android Studio或IntelliJ中尝试从Kotlin获取Java代码:
- 菜单->工具->Kotlin->显示Kotlin字节码
- 单击->反编译按钮
- 复制Java代码
通过这种方式,您可以继续使用Mockito,不幸的是,由于Kotlin处理字节码,您将获得多余的代码行(不多)。为什么不尝试在Android Studio或IntelliJ中从Kotlin获取Java代码:
- 菜单->工具->Kotlin->显示Kotlin字节码
- 单击->反编译按钮
- 复制Java代码
Function0
(此函数不带参数)、Function1
(此函数带一个参数)等等。每个接口定义一个调用方法,调用它将执行函数
在这种情况下,这两个函数都是Function1
实例,因为它们是采用一个参数的函数:
Mockito.doAnswer(invocation -> {
Function1<List<Deal>, Unit> resultListener =
(Function1<List<Deal>, Unit>) invocation.getArguments()[0];
Function1<Throwable, Unit> errorListener =
(Function1<Throwable, Unit>) invocation.getArguments()[1];
resultListener.invoke(new ArrayList<>());
return null;
}).when(api).getProductInfo(any(), any());
Mockito.doAnswer(调用->{
函数1结果列表器=
(Function1)invocation.getArguments()[0];
函数1 errorListener=
(Function1)invocation.getArguments()[1];
invoke(newarraylist());
返回null;
}).when(api).getProductInfo(any(),any());
另一方面,您也可以尝试和Mockito的团队一起用Kotlin编写测试。摘自本书: Kotlin标准库定义了一系列接口,对应于不同数量的函数参数:
Function0
(此函数不带参数)、Function1
(此函数带一个参数)等等。每个接口定义一个调用方法,调用它将执行函数
在这种情况下,这两个函数都是Function1
实例,因为它们是采用一个参数的函数:
Mockito.doAnswer(invocation -> {
Function1<List<Deal>, Unit> resultListener =
(Function1<List<Deal>, Unit>) invocation.getArguments()[0];
Function1<Throwable, Unit> errorListener =
(Function1<Throwable, Unit>) invocation.getArguments()[1];
resultListener.invoke(new ArrayList<>());
return null;
}).when(api).getProductInfo(any(), any());
Mockito.doAnswer(调用->{
函数1结果列表器=
(Function1)invocation.getArguments()[0];
函数1 errorListener=
(Function1)invocation.getArguments()[1];
invoke(newarraylist());
返回null;
}).when(api).getProductInfo(any(),any());
另一方面,您可以尝试和Mockito的团队一起用Kotlin编写测试。谢谢,就是这样:)谢谢,就是这样:)谢谢,我不知道这个有用的功能:)谢谢,我不知道这个有用的功能:)