Android Webview Chromium UnitTests fail with java.lang.NoSuchMethodError - android

We are currently facing this NoSuchMethod Error when we want to create the unit test with Robolectric version 4.3.1 and Mockito version 2.18.3
Exceptions:
java.lang.NoSuchMethodError: 'void com.android.webview.chromium.WebViewChromium.<init>(com.android.webview.chromium.WebViewChromiumFactoryProvider, android.webkit.WebView, android.webkit.WebView$PrivateAccess, boolean)'
Here is the code we did:
mWebView = Mockito.spy(
new WebviewChromium(
mockWebViewChromiumProvider, webView, webView.new PrivateAccess(), false));
In BUILD.gn, we have included the "//android_webview/glue:glue_java" as dependency, we can successfully compile the code but it sounds like in the runtime, it refers to the another wrong jar, does someone know how to config the build file to use the correct jar or any ideas about how to solve this error?

did you check this followjng thread https://github.com/robolectric/robolectric/issues/5092 and this one I think you need open jdk 11 or 13 I recently had the same issue after adding open jdk 13 to android studio it solved my problem robolectric started working fine you can check following thread as well https://github.com/robolectric/robolectric/issues/5092

Related

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

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.

How to use EasyMock in android

I use easymock-3.2.jar in my android test project.
I find it in its Home:
2013-07-11: EasyMock 3.2 is available. Add #Mock annotations and Android support.
However , I got exception when I use it.
java.lang.NoClassDefFoundError: org.easymock.EasyMock
I googled a lot , and add
dexmaker-1.0.jar
objenesis-1.2.jar
cglib-nodep-2.2.2.jar or cglib-2.2.jar
But the exception still be there.Who can help me?
Thanks a lot.
Put the library
easymock-3.2.jar
dexmaker-1.0.jar
dexmaker-mockito-1.0.jar
in tests/libs. And it will work.
Be careful , it's in tests/libs , not in tests/lib.
It will throw
java.lang.NoClassDefFoundError: org.easymock.EasyMock
if you position them in tests/lib.
The first step is to get EasyMock. You can get the latest version from Easymock’s download page. Choose the latest version and you will get a zip file. You only need easymock-3.2.jar (3.2 will change depending on the version you choose). You will also need dexmaker for Easymock to work on Android. You can get the jar from Dexmaker’s website. Once you have both jar files put them in /tests/libs. Now you have EasyMock available in your tests.
for more detail follow below link
http://ncona.com/2013/11/writing-unit-test-for-android-with-easymock/

Robolectric compilation error while using shadowOf method

I am using Robolectric 1.2 in an Andorid 2.3.3 project (API 10)
It throws the below compilation error, when using shadowOf method
ShadowView shadowView = Robolectric.shadowOf(view);
error: cannot access ObjectAnimator
But the error is gone, if I change the android sdk to API 11 and above
I'm still having this issue with 2.2. I got around this by
import static org.robolectric.Robolectric.shadowOf_;
...
ShadowView shadowView = Robolectric.shadowOf_(view);
If anyone could provide some insight why this works, that would be awesome!
After some analysis, found that it is a bug in robolectric.
For now, using 2.0-alpha-1 seems to fix the issue.
Here is the github issue created for the issue
https://github.com/pivotal/robolectric/issues/431

Problems using the EWS Java API on Android

I am trying to use the EWS Java API v1.1.5 (http://archive.msdn.microsoft.com/ewsjavaapi) in an Android application, and have run into a number of issues.
I downloaded the source, and followed the instructions provided to compile the EWS Java API in Eclipse. In those instructions you are told to download and
add the following pre-requiste jar file dependencies:
commons-codec-1.4.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
jcifs-1.3.15.jar
I did this, and followed the build instructions with produced the following jar files:
EWSAPI-1.1.0.jar
EWSAPIWithJars-1.1.0
Next, I built a brand new Android application, added the appropriate permissions to the manifest, and then added the following source to the primary activity's OnCreate:
ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials("emailaddress", "password");
service.setCredentials(credentials);
try
{
service.autodiscoverUrl("emailaddress", this);
}
catch (Exception e)
{
e.printStackTrace();
}
I first tried running this application with the EWSAPI-1.1.0.jar file as a dependency. When I did that, I obtained the following fatal error:
Uncaught handler: thread main exiting due to uncaught exception
java.lang.NoClassDefFoundError:
org.apache.commons.httpclient.MultiThreadedHttpConnectionManager at
microsoft.exchange.webservices.data.ExchangeServiceBase.(Unknown
Source) at
microsoft.exchange.webservices.data.ExchangeServiceBase.(Unknown
Source) at
microsoft.exchange.webservices.data.ExchangeService.(Unknown
Source) at
com.meshin.exchange.ExchangeDiscoveryActivity.onCreate(ExchangeDiscoveryActivity.java:40)
From what I've researched, it seems like this error is being generated because Android comes with the Apache HttpClient 4.0, which doesn't appear
to have the MultiThreadedHttpConnectionManager class anymore.
If I instead remove the EWSAPI-1.1.0.jar, and instead use the EWSAPIWithJARS-1.1.0.jar file as a dependency, I get the following error:
VFY: unable to resolve static method 908: Ljavax/xml/stream/XMLOutputFactory;
newInstance()Ljavax/xml/stream/XMLOutputFactory;
VFY: dead code 0x0008-006a in L
microsoft/exchange/webservices/data/EwsUtilities;.formatLogMessage
(Ljava/lang/String;Ljava/lang/String;) Ljava/lang/String;
VFY: unable to find class referenced in signature (Ljavax/xml/stream/XMLStreamWriter;)
And then eventually...
FATAL EXCEPTION: main java.lang.VerifyError:
microsoft.exchange.webservices.data.AutodiscoverService at
microsoft.exchange.webservices.data.ExchangeService.getAutodiscoverUrl(Unknown
Source) at
microsoft.exchange.webservices.data.ExchangeService.autodiscoverUrl(Unknown
Source) at
com.meshin.exchange.ExchangeDiscoveryActivity.onCreate(ExchangeDiscoveryActivity.java:41)
I am assuming because now I am including the HttpClient 3.1 jar and it is conflicting with the HttpClient 4.0 jar included with the Android libraries.
My question is if there is a way for me to use the EWS Java API in an Android project without having to re-write the parts of it which reference
HttpClient 3.1-specific things which are no longer in 4.0.
You can use microsoft's EWS api for android by doing the following steps,
download the source code available in the URL,
http://archive.msdn.microsoft.com/ewsjavaapi
EWSJavaAPI_1.1.5.zip
Make the changes to above api to work for JDK 1.4 in eclipse like remove override annotations e.t.c
Download source code of javax.* package available in below URL,
http://www.java2s.com/Code/JarDownload/jsr173/jsr173_1.0_src.jar.zip
Download source code of stax api available in below URL,
http://dist.codehaus.org/stax/distributions/stax-src-1.2.0.zip
Keep all the sources under the single java project in eclipse
Open the project explorer and select the package which are starts with "javax" and rename to your company name eg: com. Note: Eclipse will ask for all the naming contexts will change then click OK.
Export all the java sources to one single jar file.
Then You good to go to use the jar in Android application with out any problems.
I used the same way explained above and it worked in android application 100% perfectly.
This isn't a useful answer but I've also been struggling to look at the EWS API over the last couple of weeks, with no success.
The main issue I've found is it references a lot of the javax APIs which aren't on Android. There's a hack you can find that explains how to "re-class" these but I've not got it working yet; in addition, the DnsClient references some namespaces that jarsearches only return for rt.jar - the main runtime. And including this in an Android app would just be crazy!
Before finding the API I was trying to use KSOAP to communicate with EWS but again ran into problems - you need to be able to do NTLM and SSL and I couldn't find any way of combining all these with KSOAP in any simple way.
The EWS API looks the way to go (why re-invent the wheel) but getting it working on Android looks very tricky (if not impossible)
Please check this library for the solution

Categories

Resources