【问题标题】:NPE when i test Retrofit API with Kotlin当我用 Kotlin 测试 Retrofit API 时的 NPE
【发布时间】:2019-08-05 10:42:16
【问题描述】:

我开始学习如何编写测试并编写这个来测试我的api:

class SharedViewModelTest {

    @get:Rule
    val rule = InstantTaskExecutorRule()

    @Mock private lateinit var networkHelper: NetworkHelperImpl
    @Mock private lateinit var employeeInteractor: EmployeeInteractorImpl
    @Mock private lateinit var specialityInteractor: SpecialityInteractorImpl
    @Mock private lateinit var api:ApiService

    private lateinit var sharedViewModel: SharedViewModel

    @Before fun setUp() {
        MockitoAnnotations.initMocks(this)
        sharedViewModel = SharedViewModel(networkHelper, employeeInteractor, specialityInteractor)
    }

    @Test
    fun `check is json correct`(){
        val result = runBlocking { api.loadData().await().items }
        result shouldNotBe  null
        result.size shouldNotBe 0
    }
}

但是当我尝试运行 check is json correct 时我有这个:

java.lang.NullPointerException
    at com.example.testtask.view.viewmodel.SharedViewModelTest$check is json correct$result$1.invokeSuspend(SharedViewModelTest.kt:38)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
    at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.kt:116)
    at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:76)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:53)
    at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
    at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:35)
    at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
    at com.example.testtask.view.viewmodel.SharedViewModelTest.check is json correct(SharedViewModelTest.kt:38)

我真的无法理解为什么我有 NPE(不是服务器线程错误,NPE 完全正确)。我评论了我比较结果的行,但它仍然很糟糕,所以我认为这不是关于响应,而是其他的东西。

谁能给我解释一下?

我认为应用级别的 gradle 依赖可以提供帮助:

    //Tests
    testImplementation 'junit:junit:4.13-beta-3'
    testImplementation "org.mockito:mockito-inline:2.8.47"
    testImplementation 'org.robolectric:robolectric:4.3'
    testImplementation 'org.jetbrains.kotlin:kotlin-stdlib:1.3.41'
    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.3.41'
    testImplementation "com.nhaarman:mockito-kotlin:1.4.0"
    testImplementation 'org.amshove.kluent:kluent:1.14'
    testImplementation 'android.arch.core:core-testing:1.1.1'

【问题讨论】:

    标签: android unit-testing kotlin junit


    【解决方案1】:

    您必须先模拟您的 api 响应。例如:

    @Test
        fun `check is json correct`(){
            val result = runBlocking { 
                whenever(api.loadData()).thenReturn(async {'create your data'})
                api.loadData().await().items 
            }
            result shouldNotBe  null
            result.size shouldNotBe 0
        }
    

    【讨论】:

    • Ty,它有效。据我了解,我无法从 API 获得真正的响应来测试我的功能?
    • 您可以获得真正的响应,但在这种情况下,您不必模拟您的ApiService,而是使用真正的服务。但它会更接近集成测试。
    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2016-04-09
    • 2016-02-08
    • 2022-12-05
    • 2018-05-27
    • 2021-06-15
    • 1970-01-01
    相关资源
    最近更新 更多