niqdev / dagger-realm-test

Android tests: Dagger 2 and Realm
38 stars 8 forks source link

DatabaseRealmTest throws nullpointerexception #3

Open aemxn opened 7 years ago

aemxn commented 7 years ago

I'm new to Dagger2 and also currently learning to do unit testing with those libraries. I picked this demo repo because I think it suits my understanding towards Mockito and it really is useful for learning purpose (thanks btw).

However, when running the unit test, both test methods (test_findAll and test_add) in DatabaseRealmTest throws a NullPointerException error. I guess the error came from DatabaseRealm class not being properly injected, thus threw NPE.

public DatabaseRealm() {
   Injector.getApplicationComponent().inject(this);
}

If you have any explanation or guide on this, I would gladly appreciate it.


This is the full stacktrace logged:

java.lang.NullPointerException
    at com.aimanbaharum.perlitre.di.module.ApplicationContextModuleTest.applicationContext(ApplicationContextModuleTest.java:25)
    at com.aimanbaharum.perlitre.di.module.ApplicationContextModuleTest_ApplicationContextFactory.get(ApplicationContextModuleTest_ApplicationContextFactory.java:25)
    at com.aimanbaharum.perlitre.di.module.ApplicationContextModuleTest_ApplicationContextFactory.get(ApplicationContextModuleTest_ApplicationContextFactory.java:8)
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
    at com.aimanbaharum.perlitre.repository.DatabaseRealm_MembersInjector.injectMembers(DatabaseRealm_MembersInjector.java:29)
    at com.aimanbaharum.perlitre.repository.DatabaseRealm_MembersInjector.injectMembers(DatabaseRealm_MembersInjector.java:8)
    at com.aimanbaharum.perlitre.di.component.DaggerApplicationComponentTest.inject(DaggerApplicationComponentTest.java:131)
    at com.aimanbaharum.perlitre.repository.DatabaseRealm.<init>(DatabaseRealm.java:27)
    at com.aimanbaharum.perlitre.di.module.RepositoryModuleTest.provideDatabaseHelper(RepositoryModuleTest.java:37)
    at com.aimanbaharum.perlitre.di.module.RepositoryModuleTest_ProvideDatabaseHelperFactory.get(RepositoryModuleTest_ProvideDatabaseHelperFactory.java:24)
    at com.aimanbaharum.perlitre.di.module.RepositoryModuleTest_ProvideDatabaseHelperFactory.get(RepositoryModuleTest_ProvideDatabaseHelperFactory.java:8)
    at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
    at com.aimanbaharum.perlitre.repository.DatabaseRealmTest_MembersInjector.injectMembers(DatabaseRealmTest_MembersInjector.java:29)
    at com.aimanbaharum.perlitre.repository.DatabaseRealmTest_MembersInjector.injectMembers(DatabaseRealmTest_MembersInjector.java:7)
    at com.aimanbaharum.perlitre.di.component.DaggerApplicationComponentTest.inject(DaggerApplicationComponentTest.java:151)
    at com.aimanbaharum.perlitre.repository.DatabaseRealmTest.setupRealm(DatabaseRealmTest.java:69)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:83)
    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 org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1899)
    at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:801)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:666)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
    at org.powermock.classloading.AbstractClassloaderExecutor.getResult(AbstractClassloaderExecutor.java:69)
    at org.powermock.classloading.AbstractClassloaderExecutor.executeWithClassLoader(AbstractClassloaderExecutor.java:59)
    at org.powermock.classloading.SingleClassloaderExecutor.execute(SingleClassloaderExecutor.java:67)
    at org.powermock.classloading.AbstractClassloaderExecutor.execute(AbstractClassloaderExecutor.java:43)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:251)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
niqdev commented 7 years ago

Hi, I'll have a look, but the project isn't updated with the latest library versions and I don't have spare time at the moment. I'll check and maybe update it soon. Thanks.