Android - Mockito cannot mock this class: class android.content.Context - android

We had Mockito version 2.18.3. Now I upgraded to 3.5.13.
Also Robolectric version is upgraded from 3.8 to 4.4.
So half of 700 tests started failing on different reasons. One of them is as below that mockito can't create Context class.
Other classes it could not mock now, for ex, are many interfaces of our codebase, classes, android.app.Activity, android.location.Location.
Any help will be highly appreciated. Thanks!
net.abc.def.MyTest > FirstTest FAILED
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class android.content.Context.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Private Build
JVM vendor version : 25.265-b01
JVM name : OpenJDK 64-Bit Server VM
JVM version : 1.8.0_265-8u265-b01-0ubuntu2~16.04-b01
JVM info : mixed mode
OS name : Linux
OS version : 4.4.0-57-generic
You are seeing this disclaimer because Mockito is configured to create inlined mocks.
You can learn about inline mocks and their limitations under item #39 of the Mockito class javadoc.
Underlying exception : org.mockito.exceptions.base.MockitoException: Could not modify all classes [class android.content.Context]
at net.abc.def.MyTest.onSetUp(FirstTest.java:51)
Caused by:
org.mockito.exceptions.base.MockitoException: Could not modify all classes [class android.content.Context]
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:153)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:366)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:175)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:377)
... 1 more
Caused by:
java.lang.IllegalStateException:
Byte Buddy could not instrument all classes within the mock's type hierarchy
This problem should never occur for javac-compiled classes. This problem has been observed for classes that are:
- Compiled by older versions of scalac
- Classes that are part of the Android distribution
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:265)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.mockClass(InlineBytecodeGenerator.java:212)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:46)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator$1.call(TypeCachingBytecodeGenerator.java:43)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:153)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:366)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:175)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:377)
at org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.mockClass(TypeCachingBytecodeGenerator.java:36)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMockType(InlineByteBuddyMockMaker.java:379)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.doCreateMock(InlineByteBuddyMockMaker.java:339)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.createMock(InlineByteBuddyMockMaker.java:318)
at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:52)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
at org.mockito.Mockito.mock(Mockito.java:1949)
at org.mockito.Mockito.mock(Mockito.java:1860)
... 1 more
Caused by:
java.lang.UnsupportedClassVersionError: android/os/Bundle has been compiled by a more recent version of the Java Runtime (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.getDeclaredMethods(Class.java:1975)
at net.bytebuddy.description.method.MethodList$ForLoadedMethods.<init>(MethodList.java:109)
at net.bytebuddy.description.type.TypeDescription$ForLoadedType.getDeclaredMethods(TypeDescription.java:8715)
at net.bytebuddy.dynamic.scaffold.InstrumentedType$Factory$Default$1.represent(InstrumentedType.java:416)
at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.java:782)
at net.bytebuddy.ByteBuddy.redefine(ByteBuddy.java:757)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.transform(InlineBytecodeGenerator.java:364)
at sun.instrument.TransformerManager.transform(TransformerManager.java:188)
at sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:428)
at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method)
at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:144)
at org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.triggerRetransformation(InlineBytecodeGenerator.java:261)
... 16 more

We had 2 year old gradle version which was 3.3.1. So, when I tried to raise gradle version directly to the latest 4.x, some issues while running unit tests have popped up..so had to raise mockito also and that ended up with this problem.
Had become messy and did not know how to fix that.
Just changed the approach - started raising the gradle version one by one instead of jumping directly to the latest from such a old one. And that went smooth and all good now.
So if you are in the same situation..raise versions one by one instead of hopping long.

Related

Error with tests in Android Studio when using ReLinker to load libraries

When I try to run my unit tests, ReLinker is complaining.
I've added Bugsnag, and it works as it should. Later I add ReLinker like here: https://docs.bugsnag.com/platforms/android/faq/
After adding those two lines I am no longer able to run my tests.
My application onCreate contains:
ReLinker.loadLibrary(this, "bugsnag-ndk")
ReLinker.loadLibrary(this, "bugsnag-plugin-android-anr")
Bugsnag.start(this, config)
Whenever I try to run my tests using RobolectricTestRunner, I get the following errors:
[Robolectric] WARN: Android SDK 29 requires Java 9 (have Java 8). Tests won't be run on SDK 29 unless explicitly requested.
[Robolectric] WARN: Android SDK 30 requires Java 9 (have Java 8). Tests won't be run on SDK 30 unless explicitly requested.
Could not find 'bugsnag-ndk.dll'. Looked for: [armeabi-v7a], but only found: [java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 10
lib\([^\\]*)\bugsnag-ndk.dll
^].
com.getkeepsafe.relinker.MissingLibraryException: Could not find 'bugsnag-ndk.dll'. Looked for: [armeabi-v7a], but only found: [java.util.regex.PatternSyntaxException: Unmatched closing ')' near index 10
lib\([^\\]*)\bugsnag-ndk.dll
^].
at com.getkeepsafe.relinker.ApkLibraryInstaller.installLibrary(ApkLibraryInstaller.java:173)
at com.getkeepsafe.relinker.ReLinkerInstance.loadLibraryInternal(ReLinkerInstance.java:180)
at com.getkeepsafe.relinker.ReLinkerInstance.loadLibrary(ReLinkerInstance.java:136)
at com.getkeepsafe.relinker.ReLinker.loadLibrary(ReLinker.java:70)
at com.getkeepsafe.relinker.ReLinker.loadLibrary(ReLinker.java:51)
at com.roardam.vvitas.Vvitas.onCreate(Vvitas.kt:19)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$installAndCreateApplication$0(AndroidTestEnvironment.java:288)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:86)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:288)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:171)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:319)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:269)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
I've added ReLinker and Bugsnag with:
implementation 'com.github.KeepSafe:ReLinker:1.4.3'
implementation 'com.bugsnag:bugsnag-android:5.+'
I am not entirely sure the problem is ReLinker, but I am able to run the tests when I remove the two lines loading libraries.
Please let me know if I haven't shown enough code, and I'll update my post with whatever code needed.
Thank you.
This looks to be a Relinker problem as it occurs when loading various other native libraries not just Bugsnag.

Robolectric - Package targetSdkVersion=30 > maxSdkVersion=29

Error:
java.lang.IllegalArgumentException: failed to configure :
Package targetSdkVersion=30 > maxSdkVersion=29
at
org.robolectric.RobolectricTestRunner.getChildren(RobolectricTestRunner.java:247)
at
org.junit.runners.ParentRunner.getFilteredChildren(ParentRunner.java:534)
at
org.junit.runners.ParentRunner.getDescription(ParentRunner.java:400)
at
androidx.test.ext.junit.runners.AndroidJUnit4.getDescription(AndroidJUnit4.java:149)
at
org.junit.runners.model.RunnerBuilder.configureRunner(RunnerBuilder.java:81)
at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:72)
at
org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:37)
at
org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:70)
at
org.junit.internal.requests.ClassRequest.createRunner(ClassRequest.java:28)
at
org.junit.internal.requests.MemoizingRequest.getRunner(MemoizingRequest.java:19)
at
com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:50)
at
com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at
com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.IllegalArgumentException: Package
targetSdkVersion=30 > maxSdkVersion=29 at
org.robolectric.plugins.DefaultSdkPicker.configuredSdks(DefaultSdkPicker.java:118)
at
org.robolectric.plugins.DefaultSdkPicker.selectSdks(DefaultSdkPicker.java:69)
at
org.robolectric.RobolectricTestRunner.getChildren(RobolectricTestRunner.java:213)
... 13 more
How to fix this issue?
Found this post with the same issue, with a different error message.
This answer helped me to solve the issue.
Posting the same here.
Create a robolectric.properties file inside the app/src/test/resources directory with the following line:
sdk=29
This will force Robolectric to use API 29 instead of 30.
Note: Robolectric supports up to SDK 29 now (As on Sep 4th, 2020).
There are two ways to achieve this:
Creating prop file (mentioned in Abhimanyu's answer)
Create robolectric.properties in app/src/test/resources
Add sdk=29 in it
Limit your test target SDK using Config annotation. This makes you consider different configs for different tests or avoid creating extra config files.
#Config(sdk = [29])
class Test {
// ...
}
Both the above answers work fine, but the latest version of robolectric supports android sdk 30.
So just upgrade the robolectric dependency to fix the issue.
At the time of writing, the latest version was 4.5.1.
For the up to date version please refer the official github site.
Just update your robolectric version to 4.5 or newer, their repo notified this change:
Robolectric 4.5 adds support for Android API 30 (R final) and contains
many bug fixes and other enhancements.
Source: https://github.com/robolectric/robolectric/releases
failed to configure com.example.android.architecture.blueprints.todoapp.tasks.TasksViewModelTest.addNewTask_setsNewTaskEvent: Package targetSdkVersion=31 > maxSdkVersion=30
java.lang.IllegalArgumentException: failed to configure com.example.android.architecture.blueprints.todoapp.tasks.TasksViewModelTest.addNewTask_setsNewTaskEvent: Package targetSdkVersion=31 > maxSdkVersion=30
Had an almost similar error as above. The error was generated when I was using AndroidX Test and Robolectic APIs to execute my local unit tests. I also had included the below code to allow The testing API use the right Android manifest file during testing:
testOptions.unitTests {
includeAndroidResources = true
}
I solved the above error by downgrading my compile and target sdk from 31 to 30

Databinding NoSuchMethodError with buildtools 3.4.0

When using the latest DataBinding
classpath 'com.android.tools.build:gradle:3.4.0-alpha10'
A NoSuchMethodError crashes the app upon Activity load. Using:
classpath 'com.android.tools.build:gradle:3.2.1'
causes the databinding to work successfully.
Here's the crash:
java.lang.NoSuchMethodError: No direct method <init>
(Landroidx/databinding/DataBindingComponent;Landroid/view/View;I)V in
class Landroidx/databinding/ViewDataBinding; or its super classes
(declaration of 'androidx.databinding.ViewDataBinding'
Is there any way around this if we want to use the latest build tools?
One of your libraries relies on data binding and is distributed with generated data-binding classes built with build tools 3.3 (or earlier). The issue is caused by the breaking change introduced in the latest beta/rc version of the data binding lib. In version 3.4 the signature of androidx.databinding.ViewDataBinding constructor has been changed from:
protected ViewDataBinding(DataBindingComponent bindingComponent, View root, int localFieldCount)
to:
protected ViewDataBinding(Object bindingComponent, View root, int localFieldCount)
Which makes any generated data binding class binary incompatible with 3.4 databinding lib, resulting in the following exception upon startup:
java.lang.NoSuchMethodError: No direct method <init>(Landroidx/databinding/DataBindingComponent;Landroid/view/View;I)V in class Landroidx/databinding/ViewDataBinding; or its super classes (declaration of 'androidx.databinding.ViewDataBinding' appears in /data/app/com.example.idolon-LqF2y8dUMxZoK3PVRlzbzg==/base.apk)
at com.example.lib.databinding.ActivityLibBinding.<init>(ActivityLibBinding.java:20)
at com.example.lib.databinding.ActivityLibBindingImpl.<init>(ActivityLibBindingImpl.java:30)
at com.example.lib.databinding.ActivityLibBindingImpl.<init>(ActivityLibBindingImpl.java:27)
at com.example.lib.DataBinderMapperImpl.getDataBinder(DataBinderMapperImpl.java:316)
at androidx.databinding.MergedDataBinderMapper.getDataBinder(MergedDataBinderMapper.java:74)
at androidx.databinding.DataBindingUtil.bind(DataBindingUtil.java:199)
at androidx.databinding.DataBindingUtil.bindToAddedViews(DataBindingUtil.java:327)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:306)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
As a workaround you can rebuild libraries that contains data binding classes using the latest build tools.
The corresponding bug on Androig Bug tracker is: https://issuetracker.google.com/issues/122936785
UPDATE
The issue has been fixed and the fix is available in 3.5 beta 1 (it will also be available in the upcoming 3.4.1)

NoClassDefFoundError ObjenesisStd on Android API 19

I'm getting the following crashing during an instrumentation test ONLY on emulators running API v19. If I run on newer versions everything works fine.
03-01 20:26:18.781 2878-2878/? E/MonitoringInstrumentation: Exception
encountered by: Thread[main,5,main]. Dumping thread state to outputs
and pining for the fjords.
java.lang.NoClassDefFoundError: org.objenesis.ObjenesisStd
at org.mockito.internal.creation.jmock.ClassImposterizer.(ClassImposterizer.java:36)
at org.mockito.internal.creation.jmock.ClassImposterizer.(ClassImposterizer.java:29)
at org.mockito.internal.util.MockCreationValidator.isTypeMockable(MockCreationValidator.java:17)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:133)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:127)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:50)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)
The problem is this line:
java.lang.NoClassDefFoundError: org.objenesis.ObjenesisStd
My app is multi-dex, and I'm using dexmaker-mockito for androidTestCompile. I'm at a complete loss why this only breaks on an older API. It started happening when I added another module to my project, which is a pure java module with no dependency on mockito.
This exception (ClassNotFoundException) tells you about a unmet dependency at runtime: the JVM needs to load a class; which is not present in the class path.
Here it is Mockito that needs Objenesis. Normally that library should be pulled automatically when you a system like maven and give the correct dependency to Mockito.

Odd compiler error, Davlik error 1 from java.net.DatagramPacket.class

I suddenly started getting an unusual compiler error and is it making impossible to do any testing, I am running Eclipse with the plugin and compiling on the 2.1 version.
[2010-08-11 00:29:38 - PeriodTrackerv2]
trouble processing "java/net/DatagramPacket.class":
[2010-08-11 00:29:38 - PeriodTrackerv2]
Attempt to include a core class (java.* or javax.*) in something other
...
[2010-08-11 00:29:38 - PeriodTrackerv2] 1 error; aborting
[2010-08-11 00:29:38 - PeriodTrackerv2] Conversion to Dalvik format failed with error 1
I verified that this class does exist in the standard Android2.1 jar file, so I find it highly unusual a class in the actual base Jar file would be problematic. I am not using this class in any fashion in my program.
I updated to version 2.2 and still get same error, this completely baffles me and just appeared tonight after zero issues for months.
Any ideas on how to fix this?
Cheers,
Tom
Alright!
I reviewed the build path and somehow the Android.jar was added twice. Problem solved.

Categories

Resources