Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null Pointer Exception When Mocking Method With No Parameters in Android Ui Test #794

Closed
simon1867 opened this issue Nov 29, 2016 · 4 comments
Labels

Comments

@simon1867
Copy link

I get a crash when I mock a method in an interface that doesn't take any parameters. This only happens in Android Espresso tests. I've replicated the same conditions in a unit test with robolectric and that problem doesn't exist.

I created a repo that has a ui test with the crash
https://github.com/simon-the-canadian/mockito-android-ui-test-null-pointer

java.lang.NullPointerException: Attempt to get length of null array
at org.mockito.internal.invocation.MatcherApplicationStrategy.getMatcherApplicationType(MatcherApplicationStrategy.java:90)
at org.mockito.internal.invocation.MatcherApplicationStrategy.getMatcherApplicationStrategyFor(MatcherApplicationStrategy.java:53)
at org.mockito.internal.invocation.InvocationMatcher.argumentsMatch(InvocationMatcher.java:152)
at org.mockito.internal.invocation.InvocationMatcher.matches(InvocationMatcher.java:81)
at org.mockito.internal.stubbing.InvocationContainerImpl.findAnswerFor(InvocationContainerImpl.java:78)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:88)
at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:32)
at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:36)
at com.android.dx.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAdapter.java:61)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy5.getSampleValue(Unknown Source)
at myawsomedomain.com.myapplication.UiTest.employeeSearchTest(UiTest.java:40)
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 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
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 android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
@ChristianSchwarz
Copy link
Contributor

Seems like the Invocation.getRawArguments() returns illegaly an null array. I looked into it and found 3 places in package org.mockito.internal.creation.bytebuddy were an Invocation instance is created with raw arguments:

  • MockMethodAdvice.handle(Object, Method, Object[])
  • MockMethodInterceptor.DispatcherDefaultingToRealMethod.interceptAbstract(Object, MockMethodInterceptor, Object, Method, Object[])
  • MockMethodInterceptor.DispatcherDefaultingToRealMethod.interceptSuperCallable(Object, MockMethodInterceptor, Method, Object[], Callable<?>)

@raphw Do you have an idea why an null array is passed here?

@raphw
Copy link
Member

raphw commented Nov 30, 2016

This does not look possible from the code. Byte Buddy never assigns null to AllArguments either. At the same time, from ArgumentsProcessor::expandVarArgs, it seems like args being null is a legitimate case.

As this is run in an instrumentation test, it is neither Byte Buddy which is creating the byte code, it is a fork of dexmaker which is causing the problem: https://github.com/blueapron/dexmaker

This problem does not occur with mockito-android which we still have not released.

@raphw raphw closed this as completed Nov 30, 2016
@simon1867
Copy link
Author

Do you guys have an idea for the timeframe when mockito-android will be released?

@TimvdLippe
Copy link
Contributor

Soon rather than later. We are still tinkering out the publication mechanism. E.g. separate artifact, separate repo?

@bric3 bric3 added the android label Jan 19, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants