Kotlin单元测试assertEquals无效

时间:2019-01-16 13:29:06

标签: android unit-testing kotlin

我正在尝试在Android Studio中编写单元测试代码。

在调试模式下,当我到达断点时,我具有以下变量:

token!!.length = 392

以下断言永远不会成立:

assertEquals(392, token!!.length)

我不知道那怎么可能。

“运行”输出为:

java.lang.AssertionError: 
Expected :392
Actual   :0
 <Click to see difference>

    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:144)
    at com.activity.screen.myapp.ExampleUnitTest.get_auth_token_isCorrect(ExampleUnitTest.kt:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

janv. 16, 2019 2:29:40 PM okhttp3.internal.platform.Platform log
INFOS: --> POST https://theauthwebsite/api/auth/token http/1.1

Process finished with exit code -1

源代码:

    var token: String? = "" 

    var user = user("support@mycompany.fr","aaaabbbb", "1") 

    AuthService.getAuthToken(user).enqueue(object : Callback<Result> { 

        override fun onResponse(call: Call<Result>, response: Response<Result>) { 
            if (response.isSuccessful) { 
                token = response.body()!!.result!!.token 
            } 
        } 

        override fun onFailure(call: Call<Result>, t: Throwable) { 
            token = ""; 
        } 
    }) 

    assertEquals(392, token!!.length)  // Here token really has a length of 392 so that it should be ok but it is not
}

编辑:解决方案是使用同步请求,因此代码被以下代码修改:

token = authServices.getAuthToken(user).execute().body()!!.result!!.token

assertEquals(392, token!!.length)

谢谢你们!

2 个答案:

答案 0 :(得分:2)

您不等待结果。 enqueue()方法异步执行请求。这意味着该回调将不会在同一线程上发生。进行enqueue()调用后,断言将立即被调用。此时,尚未调用任何回调,因此token的值尚未更改。如果要同步等待结果,请使用execute()而不是enqueue()

答案 1 :(得分:2)

您断言为时过早。当测试到达断言时,异步令牌请求尚未完成。

enqueue()闻起来像翻新。改用execute()使通话同步。