Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Matchers.anyObject()验证挂起函数_Android_Junit_Mockito_Kotlinx.coroutines - Fatal编程技术网

Android 使用Matchers.anyObject()验证挂起函数

Android 使用Matchers.anyObject()验证挂起函数,android,junit,mockito,kotlinx.coroutines,Android,Junit,Mockito,Kotlinx.coroutines,我试图将协同路由添加到我们的Android应用程序中,但我们的模拟框架遇到了障碍。我的界面具有如下暂停功能: interface MyInterface { suspend fun makeNetworkCall(id: String?) : Response? } 下面是我如何尝试验证代码是否在单元测试中执行的 runBlocking { verify(myInterface).makeNetworkCall(Matchers.anyObject()) } 当我这样做的时候,我得到

我试图将协同路由添加到我们的Android应用程序中,但我们的模拟框架遇到了障碍。我的界面具有如下暂停功能:

interface MyInterface {
  suspend fun makeNetworkCall(id: String?) : Response?
}
下面是我如何尝试验证代码是否在单元测试中执行的

runBlocking {
  verify(myInterface).makeNetworkCall(Matchers.anyObject())
}
当我这样做的时候,我得到了以下错误

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at     com.myproject.MyTest$testFunction$1.invokeSuspend(MyTest.kt:66)

This exception may occur if matchers are combined with raw values:
  //incorrect:
  someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
  //correct:
  someMethod(anyObject(), eq("String by matcher"));

在使用协同路由时,是否有其他方法可以验证是否调用了适当的方法?任何帮助都将不胜感激。

我尝试使用您提供的代码编写类似的测试。起初,我和你犯了同样的错误。然而,当我使用mockito core v2.23.4时,测试通过了

以下是您可以尝试的快速步骤:

  • testCompile“org.mockito:mockito核心:2.23.4”
    添加到build.gradle文件的依赖项列表中

  • 再次运行测试,您不应该得到类似的错误

  • 由于不推荐使用
    Matchers.anyObject()
    ,我使用了
    ArgumentMatchers.any()

    您可以在下面看到客户端代码:

    data class Response(val message: String)
    
    interface MyInterface {
        suspend fun makeNetworkCall(id: String?) : Response?
    }
    
    class Client(val  myInterface: MyInterface) {
        suspend fun doSomething(id: String?) {
            myInterface.makeNetworkCall(id)
        }
    }
    
    以下是测试代码:

    class ClientTest {
        var myInterface: MyInterface = mock(MyInterface::class.java)
    
        lateinit var SUT: Client
    
        @Before
        fun setUp() {
            SUT = Client(myInterface)
        }
    
        @Test
        fun doSomething() = runBlocking<Unit> {
            // Act
            SUT.doSomething("123")
            // Verify
            Mockito.verify(myInterface).makeNetworkCall(ArgumentMatchers.any())
        }
    }
    
    class客户端测试{
    var myInterface:myInterface=mock(myInterface::class.java)
    lateinit var SUT:客户端
    @以前
    趣味设置(){
    SUT=客户端(myInterface)
    }
    @试验
    fun doSomething()=运行阻塞{
    //表演
    SUT.剂量测定法(“123”)
    //核实
    Mockito.verify(myInterface).makeNetworkCall(ArgumentMatchers.any())
    }
    }
    
    谢谢您的回复。出于某种原因,我在运行提供的示例时遇到以下错误:java.lang.NoSuchMethodError:org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress()Lorg/mockito/internal/progress/mockingProgress;这是stacktrace中引用代码的行。在com.myproject.TestingStuff$doSomething$1.invokeSuspend(TestingStuff.kt:37)上,您是否将mockito更新为v2.23.4?我试过Intellij Idea和Android Studio,但没有出现类似的错误。也许你可以尝试清理和重建?谢谢,你帮我节省了3个小时