question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

{HILT] Testing with hilt is crashing

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
bcorsocommented, Sep 30, 2020

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?

0reactions
tinder-levonpetrosyancommented, Feb 1, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Hilt Crashes on Startup - kotlin - Stack Overflow
After tinkering around with different version values it looks like downgrading the app dependency classpath "com.google.dagger:hilt-android- ...
Read more >
Hilt testing guide | Android Developers
This makes Hilt work for all of the instrumented tests in your project. Perform the following steps:
Read more >
[Solved]-Hilt - Dependency cycle crash-kotlin - appsloveworld
Coding example for the question Hilt - Dependency cycle crash-kotlin. ... How to mock the view model with Hilt for unit testing fragments?...
Read more >
Testing - Hilt - Dagger
Note: Currently, Hilt only supports Android instrumentation and Robolectric tests (although, see here for limitations when running Robolectric tests via ...
Read more >
Full Guide to Testing Android Applications in 2022
Setting up Hilt testing; Replacing Hilt modules for tests ... mapping won't have any sources and will crash with a NullPointerException .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found