【问题标题】:Spek + Retrofit api test crashingSpek + Retrofit api测试崩溃
【发布时间】:2018-04-26 13:55:28
【问题描述】:

我正在尝试使用 Spek 测试 Retrofit api

它在 on{...} 块上抛出 nullPointerException

相关的堆栈跟踪:https://pastebin.com/gy6dLtGg

这是我的测试课

@RunWith(JUnitPlatform::class)
class AccountCheckViewModelTest : Spek({

    include(RxSchedulersOverrideRule)

    val httpException = mock<HttpException> {
        on { code() }.thenReturn(400)
    }

    given(" account check view model") {
        var accountCheckRequest = mock<CheckExistingAccountRequest>()
        var accountCheckResponse = mock<CheckExistingAccountResponse>()
        var webService = mock<IAPICalls>()

        val accountCheckViewModel = spy(VMAccountCheck(webService))

        beforeEachTest {
            accountCheckRequest = mock<CheckExistingAccountRequest>() {
                on { email }.thenReturn("foo@mail")
            }

            accountCheckResponse = mock<CheckExistingAccountResponse>() {
                on { firstName }.thenReturn("foo")
                on { email }.thenReturn("foo@mail")
            }

            webService = mock<IAPICalls> {
                on { checkExistingAccount(accountCheckRequest) }.thenReturn(Flowable.just(accountCheckResponse))
            }
         }
        on("api success") {
            accountCheckViewModel.checkIfAccountExists(request = accountCheckRequest)

            it("should call live data with first name as foo") {
               verify(accountCheckViewModel, times(1)).updateLiveData(accountCheckResponse.firstName, accountCheckResponse.email, null)
            }
        }
    }
}

这是我的 RxSchedulersOverrideSpek 类

 class RxSchedulersOverrideSpek : Spek({

    beforeGroup {
        RxJavaPlugins.onIoScheduler(Schedulers.trampoline())
        RxJavaPlugins.onComputationScheduler(Schedulers.trampoline())
        RxJavaPlugins.onNewThreadScheduler(Schedulers.trampoline())
    }
})

【问题讨论】:

  • 请注意您在beforeGroup 中遗漏了RxAndroidPlugins.setInitMainThreadSchedulerHandler( __ -&gt; Schedulers.trampoline());
  • 是 - NPE 是因为缺少 RxAndroidPlugins.setInitMainThreadSchedulerHandler()

标签: android kotlin retrofit2 rx-java2 spek


【解决方案1】:

您应该使用memoized 正确设置测试值。问题是 accountCheckViewModel 在 Spek 的发现阶段被初始化,传递给 accountCheckViewModelwebService 模拟是当时的值(你没有模拟它的任何方法)。 beforeEachTest 在执行阶段运行,您已在此处将 webService 重新分配给正确的模拟,但 accountCheckViewModel 仍保留以前的值。

given(" account check view model") {
  val accountCheckRequest by memoized {
    mock<CheckExistingAccountRequest>() {
      on { email }.thenReturn("foo@mail")
    }
  }
  val accountCheckResponse by memoized {
    mock<CheckExistingAccountResponse>() {
      on { firstName }.thenReturn("foo")
      on { email }.thenReturn("foo@mail")
    }
  }
  val webService by memoized {
    mock<IAPICalls> {
      on { checkExistingAccount(accountCheckRequest) }.thenReturn(Flowable.just(accountCheckResponse))
    }
  }

  val accountCheckViewModel by memoized {
    spy(VMAccountCheck(webService))
  }

  on("api success") {
    accountCheckViewModel.checkIfAccountExists(request = accountCheckRequest)

    it("should call live data with first name as foo") {
      verify(accountCheckViewModel, times(1)).updateLiveData(accountCheckResponse.firstName, accountCheckResponse.email, null)
    }
  }
}

【讨论】:

    【解决方案2】:

    假设您将 RxJava2RxAndroid 一起使用,您应该使用 Schedulers.trampoline() 覆盖 RxAndroid 调度程序。这样所有在 trampoline() 上订阅的作业都将在同一个线程中排队并一一执行。

    RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
    

    您的RxSchedulersOverrideSpek.kt 应如下所示:

    object RxSchedulersOverrideSpek : Spek({
    
        beforeGroup {
            RxJavaPlugins.onIoScheduler(Schedulers.trampoline())
            RxJavaPlugins.onComputationScheduler(Schedulers.trampoline())
            RxJavaPlugins.onNewThreadScheduler(Schedulers.trampoline())
            RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
        }
    
        afterGroup {
            RxJavaPlugins.reset()
            RxAndroidPlugins.reset()
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      • 1970-01-01
      相关资源
      最近更新 更多