首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >存根时出现空指针异常

存根时出现空指针异常
EN

Stack Overflow用户
提问于 2020-11-02 19:07:54
回答 1查看 361关注 0票数 0

下午好,我在存根时有一个空指针异常

代码语言:javascript
运行
复制
package com.micheladrien.android.fresquerappel

import android.app.Application
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.micheladrien.fresquerappel.R
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock    
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.junit.MockitoJUnitRunner


@RunWith(MockitoJUnitRunner::class)
class MainViewModelTest {

  @Rule
  @JvmField
  var instantTaskExecutorRule = InstantTaskExecutorRule()    

  @Mock
  val applicationMock: Application = mock(Application::class.java)
 
  @Before
  fun setUpTaskDetailViewModel() {
        `when`(applicationMock.getString(R.string.collage_climat)).thenReturn("Climat")
    }
    }

编辑:我需要存根函数when(applicationMock.getString(R.string.collage_climat)).thenReturn("Climat"),因为我的视图模型将从上下文中获取字符串。

根据这篇博文:https://codepills.com/2018/05/10/3-basic-mistakes-for-nullpointerexception-when-mock/我应该在返回时替换为when then (回答),如果这是真的,为什么?

我已经测试过:将R.id值更改为一个不正确的数字。模拟before中的文件并在@Mock =>中使用=>声明时出现相同的错误

与前面的问题线程Mockito - NullpointerException when stubbing Method不同,我直接截断了这个方法。不截断另一个方法的对象的方法。

任何帮助都将不胜感激。

编辑:我要测试的虚拟机:

代码语言:javascript
运行
复制
class MainViewModel(application: Application): AndroidViewModel(application), WaitingViewModel{

    private val _name = MutableLiveData<String>().apply {
        value = application.getString(R.string.collage_climat)
    }

    val name : LiveData<String> = _name

    override fun notifyNewCollage(collage_name: String) {
        _name.value = collage_name
    }

}

如果你想检查Mockito的版本,这里是gradle:

代码语言:javascript
运行
复制
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.3'
def navigation_version = '2.3.1'
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "androidx.navigation:navigation-fragment:$navigation_version"
implementation "androidx.navigation:navigation-ui:$navigation_version"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
implementation 'il.co.theblitz:observablecollections:1.4.2'
def espressocore_version = '3.3.0'
androidTestImplementation "androidx.test.espresso:espresso-core:$espressocore_version"
androidTestImplementation "androidx.test.espresso:espresso-core:$espressocore_version"
androidTestImplementation "android.arch.core:core-testing:$lifecycle_version"
def mockito_version = '3.5.5' // For local unit tests on your development machine (also tested on 3.3.3)
testImplementation "org.mockito:mockito-core:$mockito_version" // For instrumentation tests on Android devices and emulators
androidTestImplementation "org.mockito:mockito-android:$mockito_version"
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-12 17:04:31

Mockito建议不要嘲笑你不拥有的类。因此,应用程序模拟不是一个好主意。Junit可以为您提供所需的应用程序上下文:https://developer.android.com/training/testing/junit-runner

有关存根失败的其他信息,请访问Mockito fails on stubbing : it tries to execute the function that should be stubbed

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64644388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档