web-dev-qa-db-fra.com

MutableLiveData est null dans JUnitTest

Je veux écrire un test unitaire. J'ai donc besoin de MutableLiveData. J'ai commencé avec un test très basique de configuration mais je ne peux pas instancier un objet MutableLiveData. I est toujours nul lorsque je lance le test. Dois-je me moquer de quelque chose? Aucune suggestion?

@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)

build.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'
}
11
Kewitschka

Il semble que vous n'ayez pas la dépendance Android.Arch.core: core-testing.

testImplementation "Android.Arch.core:core-testing:1.1.1"

Cela vous permet d'utiliser InstantTaskExecutorRule dans votre test, ce qui supprimera l'appel isMainThread.

https://developer.Android.com/reference/Android/Arch/core/executor/testing/InstantTaskExecutorRule.html

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
21
Chris