0
をあざけるテストアンドロイドを実行したとき、私はテストを作成します。のNullポインタ
class MenuPresenterTest {
lateinit var presenter: MenuPresenter
lateinit var view: MenuView
@Before fun setUp() {
view = mock(MenuView::class.java)
presenter = MenuPresenter()
}
@Test fun test_validation() {
presenter.openActivity()
verify(view).openActivity()
}
}
MenuView
interface MenuView : MvpView {
fun init()
fun openActivity()
}
MenuPresenter
@PerActivity
class MenuPresenter @Inject constructor() : MvpPresenter<MenuView>() {
fun initView(){
view.init()
}
fun openActivity(){
view.openActivity()
}
}
MvpPresenter
open class MvpPresenter<V : MvpBaseView> : MvpBasePresenter<V>() {
// things specific for current project
override fun bindView(view: V) {
super.bindView(view)
}
override fun unbindView() {
super.unbindView()
clear()
}
}
MvpBasePresenter:私は、テストを実行すると
abstract class MvpBasePresenter<V : MvpBaseView> {
private var viewRef: WeakReference<V>? = null
private val disposables = CompositeDisposable()
fun clear() = disposables.clear()
fun dispose(disposable: Disposable) = disposables.add(disposable)
open fun bindView(view: V) {
viewRef = WeakReference(view)
}
open fun unbindView() {
viewRef = null
}
fun isViewBound() = viewRef != null
val view: V
get() {
if (viewRef == null) {
throw NullPointerException("getView() called when viewRef is null. Ensure bindView(View view) is called first.")
} else {
return viewRef!!.get() as V
}
}
}
私が手:
java.lang.NullPointerException: getView() called when viewRef is null. Ensure bindView(View view) is called first.
at base.MvpBasePresenter.getView(MvpBasePresenter.kt:27)
任意のアイデアなぜ?
それはそれです!私は呼び出しメソッドの前にテストクラスのpresenter.bindView(view)を追加するのを忘れてしまった。ありがとう! – edi233