google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.41k stars 2.01k forks source link

{HILT] Testing with hilt is crashing #2101

Closed cooldude77 closed 3 years ago

cooldude77 commented 3 years ago

Hi,

So I am trying to start an activity and do a mock injection using HILT and ESPRESSO. Upon running the test , I get this strange error This was not so until I started using hilt and mocking database dependencies in the project.

E/System: Unable to open zip file: /data/user/0/com.*****/cache/oestHVv0.jar
E/System: java.io.FileNotFoundException: /data/user/0/com.*****/cache/oestHVv0.jar (No such file or directory)
        at java.util.zip.ZipFile.open(Native Method)
        at java.util.zip.ZipFile.<init>(ZipFile.java:265)
        at java.util.zip.ZipFile.<init>(ZipFile.java:187)
        at java.util.jar.JarFile.<init>(JarFile.java:169)
        at java.util.jar.JarFile.<init>(JarFile.java:106)
        at libcore.io.ClassPathURLStreamHandler.<init>(ClassPathURLStreamHandler.java:46)
        at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:777)
        at dalvik.system.DexPathList$Element.findResource(DexPathList.java:804)
        at dalvik.system.DexPathList.findResources(DexPathList.java:599)
        at dalvik.system.BaseDexClassLoader.findResources(BaseDexClassLoader.java:248)
        at java.lang.ClassLoader.getResources(ClassLoader.java:839)
        at java.util.ServiceLoader$LazyIterator.hasNextService(ServiceLoader.java:349)
        at java.util.ServiceLoader$LazyIterator.hasNext(ServiceLoader.java:402)
        at java.util.ServiceLoader$1.hasNext(ServiceLoader.java:488)
        at androidx.test.internal.platform.ServiceLoaderWrapper.loadService(ServiceLoaderWrapper.java:46)
        at androidx.test.internal.platform.ServiceLoaderWrapper.loadSingleService(ServiceLoaderWrapper.java:69)
        at androidx.test.core.app.ActivityScenario.<init>(ActivityScenario.java:145)
        at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:206)
        at com.instanect.task.MainActivityTest.init(MainActivityTest.kt:48)
        at java.lang.reflect.Method.invoke(Native Method)
        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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:76)
        at dagger.hilt.android.internal.testing.MarkThatRulesRanRule$1.evaluate(MarkThatRulesRanRule.java:92)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        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.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 org.junit.runner.JUnitCore.run(JUnitCore.java:115)
        at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
        at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2214)

Similar issue here https://stackoverflow.com/questions/58061693/espresso-generating-filenotfoundexception-when-used-with-dagger

bcorso commented 3 years ago

If you have a minimal reproducing example we can try to take a look. Just from the stack trace it's difficult to say if it's due to Dagger/Hilt because the error is occurring within ActivityScenario.<init>. FWIW, I found a similar issue on SO which has this issue when using just ActivityScenario and not Dagger.

cooldude77 commented 3 years ago

Hey, I have opened up my repo for your testing. Please run testActivityStartedof MainActivityTestto check it . Thanks a lot

bcorso commented 3 years ago

Hi @cooldude77, thanks for the repo.

I did get an error when running testActivityStarted, but it was a different error:

org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.instanect.task.business_layer.database.TaskDatabaseHelper.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

IMPORTANT INFORMATION FOR ANDROID USERS:

The regular Byte Buddy mock makers cannot generate code on an Android VM!
To resolve this, please use the 'mockito-android' dependency for your application:

Following the directions in the error message above, I was able to fix this by swapping this with:

   androidTestImplementation group: 'org.mockito', name: 'mockito-android', version: '2.22.0'

I'm not sure if that change will also get you past your issue as well, but once that is fixed I get a new error:

java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?

Which makes sense because there is no activity being launched in MainActivityTest.

Did you mean to add an ActivityScenario or ActivityScenarioRule to launch an activity in this test?

cooldude77 commented 3 years ago

Hey @bcorso

Thanks for checking this out and replying. Sorry for the delay.

I was struggling with problem that you had identified. The test case resolved to satisfaction . The exception with jar file is not gone though and I have placed a ticket with android. I don't think that HILT is causing that problem at all.

I will keep my repo open for 1-2 weeks in case you need to take a look at it again.

Thanks again

tinder-levonpetrosyan commented 2 years ago

Hey @cooldude77 , what actually resolved the issue with the exception of the jar file? Were your tests failing because of the exception?