26

我想写一个单元测试。因此我需要 MutableLiveData。我从一个非常基本的设置测试开始,但我无法实例化 MutableLiveData 对象。运行测试时,我总是为空。我必须嘲笑什么吗?有什么建议么?

@RunWith(MockitoJUnitRunner.class)
public class DefaultLiveDataTest {

    private static final int EXPECTED = 5;

    private final MutableLiveData<Integer> underTest = new MutableLiveData<>();


    @Test
    public void exampleTest() {

    underTest.setValue(EXPECTED); //underTest is Null
    assertEquals(underTest.getValue().intValue(), EXPECTED);

    }
}

java.lang.NullPointerException
at android.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:58)
at android.arch.core.executor.ArchTaskExecutor.isMainThread(ArchTaskExecutor.java:116)
at android.arch.lifecycle.LiveData.assertMainThread(LiveData.java:434)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:279)
at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
at com.mypackage.DefaultLiveDataTest.test_that_live_data_has_default_value(DefaultLiveDataTest.java:22)

构建.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId 'com.mypackage.title'
    minSdkVersion 16
    targetSdkVersion 27
    versionCode 1
    versionName '1.0'
    testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}

testOptions {
    unitTests.returnDefaultValues = true
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-vector-drawable:27.1.1'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'android.arch.lifecycle:viewmodel:1.1.1'
implementation 'android.arch.lifecycle:livedata:1.1.1'

annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
annotationProcessor 'org.androidannotations:androidannotations:4.4.0'
implementation 'org.androidannotations:androidannotations-api:4.4.0'
compileOnly 'org.projectlombok:lombok:1.16.20'
annotationProcessor 'org.projectlombok:lombok:1.16.20'
}
4

3 回答 3

51

看起来您缺少 android.arch.core:core-testing 依赖项。

testImplementation "android.arch.core:core-testing:1.1.1"

这允许您在测试中使用 InstantTaskExecutorRule,这将摆脱 isMainThread 调用。

https://developer.android.com/reference/android/arch/core/executor/testing/InstantTaskExecutorRule.html

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
于 2018-04-15T09:51:04.800 回答
12

添加一个执行器InstantTaskExecutorRule()作为测试类的成员

一个 JUnit 测试规则,它将架构组件使用的后台执行程序与同步执行每个任务的不同执行程序交换。您可以将此规则用于使用架构组件的主机端测试。

//@RunWith(JUnit4::class)   // For JUnit4
@ExtendWith(InstantExecutorExtension::class)   // For JUnit5
class FilterViewModelTest {

    @Rule @JvmField
    val instantTaskExecutorRule = InstantTaskExecutorRule()

    @Test
    fun test() {
        //Here you don't ask if isMainThread
    }
}

build.gradle(:mobile)

android {
    //...
    dependencies {
        //...
        testImplementation 'androidx.arch.core:core-testing:2.1.0'
        androidTestImplementation 'androidx.arch.core:core-testing:2.1.0'
    }
}

总帐

InstantTaskExecutorRule

于 2020-05-29T00:51:40.793 回答
1

我有这个错误并通过添加 InstantTaskExecutorRule 解决了它:

private lateinit var contactProfileViewModel: ContactProfileViewModel

private val getStatusesForContact: GetStatusesForContact = mockk(relaxed = true)
private val getStory: GetUserLastStory = mockk(relaxed = true)
private val successStatusesCaptor = slot<((List<StatusDomain>) -> Unit)>()
private val successStoryCaptor = slot<((List<StoryDomain>) -> Unit)>()

@get:Rule
val rule: TestRule = InstantTaskExecutorRule()

@Before
fun setUp(){
    contactProfileViewModel = ContactProfileViewModel(getStatusesForContact, getStory)
}
于 2020-08-18T12:54:19.710 回答