You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class LoginActivityTest {
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
@Mock
SomeClass someClass;
@Mock
SomeOtherClass someOtherClass;
ActivityController<TestLoginActivity> loginActivityActivityController =
Robolectric.buildActivity(TestLoginActivity.class);
@InjectMocks
TestLoginActivity testLoginActivity = loginActivityActivityController.get();
@Test
public void testSomething() {}
public static class TestLoginActivity extends android.app.Activity {
SomeClass someClass;
SomeOtherClass someOtherClass;
}
}
the injection fails with the following error:
Mockito couldn't inject mock dependency on field 'private java.lang.Object android.app.Activity.mInstanceTracker' that is annotated with @InjectMocks in your test,
because there were multiple matching mocks (i.e. fields annotated with @Mock and having matching type): someClass.
If you have multiple fields of same type in your class under test then consider naming the @Mock fields identically to the respective class under test's fields, so Mockito can match them by name.
at app//org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at app//org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:588)
at app//org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$2(SandboxTestRunner.java:290)
at app//org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:101)
at java.base@11.0.20.1/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base@11.0.20.1/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base@11.0.20.1/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base@11.0.20.1/java.lang.Thread.run(Thread.java:829)
Issue here is that Android's android.app.Activity has this mInstanceTracker as private variable that, because it is of type Object, matches every possible mock. I think Mockito's approach to inject things here is a little too greedy; Mockito should not scan library files for injectable fields or - since this is not really detectable - at least have some kind of package filter option / possibility to rule out foreign classes that should not be touched.
The text was updated successfully, but these errors were encountered:
Given the following code
the injection fails with the following error:
Issue here is that Android's
android.app.Activity
has thismInstanceTracker
as private variable that, because it is of typeObject
, matches every possible mock. I think Mockito's approach to inject things here is a little too greedy; Mockito should not scan library files for injectable fields or - since this is not really detectable - at least have some kind of package filter option / possibility to rule out foreign classes that should not be touched.The text was updated successfully, but these errors were encountered: