1

我正在尝试使用 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())
    }
})
4

2 回答 2

1

您应该使用memoized正确设置测试值。问题是accountCheckViewModel在 Spek 的发现阶段初始化的,webService传递给的模拟是accountCheckViewModel当时的值(你没有模拟它的任何方法)。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)
    }
  }
}
于 2018-04-27T02:01:06.073 回答
0

假设您将RxJava2RxAndroid一起使用,您应该使用Schedulers.trampoline(). 这样,所有在 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()
    }
})
于 2018-05-02T20:25:42.660 回答