mardi 2 octobre 2018

How to use Application Object which init dagger with junit testing

Trying to start implementing testing for my application class which init dagger and instantiate appComponent with AppModule and Storage Module, I found that extends ApplicationTestCase should help and setup the application but the problem getApplication() always return null. Is it the right way to use the application object in junit and why getApplication() return null ?

  class LirisApplication : Application() {

  companion object {
    lateinit var appComponent: AppComponent
  }

  fun getDataComponent(): AppComponent {
    return appComponent
  }

  override fun onCreate() {
    super.onCreate()
    initializeDagger()
    Fabric.with(this, Crashlytics())
  }

  private fun initializeDagger() {
    appComponent = DaggerAppComponent.builder()
            .appModule(AppModule(this))
            .storageModule(StorageModule())
            .networkModule(NetworkModule(BuildConfig.BASE_URL))
            .build()
    }

   }

ApplicationTest

public class ApplicationTest extends ApplicationTestCase<LirisApplication> {

    private LirisApplication application;

    public ApplicationTest() {
        super(LirisApplication.class);
    }

    protected void setUp() throws Exception {
        super.setUp();
        createApplication();
        application = getApplication();
    }

    public void testCorrectVersion() throws Exception {
        PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
        assertNotNull(info);
        MoreAsserts.assertMatchesRegex("\\d\\.\\d", info.versionName);
    }

}

The error is in this line because the application is null

PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);

Log

java.lang.NullPointerException
    at digitu.com.osmos.ApplicationTest.testCorrectVersion(ApplicationTest.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)

Aucun commentaire:

Enregistrer un commentaire