I am writing a test using Easy Mock and I get this error.
I have added all the required jar files Objenesis and Cglib.
Can anyone tell me why I am getting this error?
java.lang.NoClassDefFoundError: org.easymock.EasyMock
at de.uitool.commons.api.model.IconTextActionViewFactoryImplTest.setUp(IconTextActionViewFactoryImplTest.java:26)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
My setUp() method:
protected void setUp() throws Exception {
super.setUp();
actionHandlerProvider = EasyMock.createMock(ActionHandlerProvider.class);
}
I have found this solution for setting up AndroidMock. You can download the required jar files and also a pdf which shows the required steps to set up mock tests for android.
Android Mock
Related
We haven an Android project where we use MockitoTestRunner and RobolectricTestRunner for different kinds of tests.
I have written a set of unit test that are concerned with SSL, thus loading certs/keystores/truststores, etc. For this I used the MockitoJUnitRunner and added the Bouncycastle provider programmatically as such:
Security.insertProviderAt(new BouncyCastleProvider(), 1);
Now, these tests run perfectly fine when run on their own - e.g. when I directly run single methods from the test classes, or run these classes from the Project tree menu, they work just fine.
But when I run said tests along side ANY test which uses the RobolectricTestRunner (such as if I just run all the tests in my project together before committing), I get the following exception:
java.io.IOException: error constructing MAC:
java.lang.SecurityException: JCE cannot authenticate the provider BC
I'm baffled. How will the testrunner used in one test class affect the run of other classes, especially if we use a different test runner?
Additional info:
The exception only occurs once I actually try do do something with the BC provider (e.g. the first time the test tries to load a PKCS12 certificate) - the insertProviderAt(...) call itself seems to pass fine...
Also when I print out the list of providers for each test run, I see that Robolectric already has a BC provider in there, but is still failing when I try to use it.
Also if I don't add the BC provider, the tests still fail with the same error when run in a test suite alongside Robolectric tests. When run alone they fail with java.security.NoSuchProviderException: no such provider: BC, as we're specifying the provider explicitly.
Seems like Robolectric is using its own classloader (that favors its replacements on the Android API), which could be in conflicts with the regular classloader of Mockito.
So for using at the same time the Robolectric and mockito, you may do the following:
Make use of the Robolectric runner. Robolectric uses its own classloader that favors its replacements for the Android API, so it really does need to handle classloading on its own. There's no other way to use Robolecric.
Replace the #RunWith(MockitoJUnitRunner.class) with these alternative methods covering the behaviour of MockitoJUnitRunner:
#Before public void setUpMockito() {
MockitoAnnotations.initMocks(this);
}
#After public void tearDownMockito() {
Mockito.validateMockitoUsage();
}
Maybe it could be a workaround for using the Robolectric classloader and Mockito at the same time.
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.
I have been using robotium to test my android application. I found it very useful tool so far. Recently we have done a refactoring that would use only one activity in the entire application, each page will be replaced by a fragment.
However, After we start using that activity to run the unit tests, the test complains NoClassDefound error -- it couldn't find the activity class. I don't see anywhere I have change the configuration whatsoever.
Can anybody give a clue what might be wrong , where to check and so on ?
[INFO] java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(T estSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
....
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.xxx.wallet.HaloActivity
at com.xxx.wallet.HaloActivityTest.<init>(HaloActivityTest.java:12)
... 18 more
The app apk is loaded, AndroidManifest.xml should be ok too.
Make sureafter refactoring:
The AndroidManifest.xml of the test project is still accurate:
<instrumentation android:targetPackage="package.of.the.app.under.test">
The Test class is still accurate:
public class YourTest extends ActivityInstrumentationTestCase2<SplashScreenActivity> {
protected static final String TARGET_PACKAGE_ID = "package.of.the.app.under.test";
protected Solo solo;
public Test() {
super(TARGET_PACKAGE_ID, StartingActivityOfYourAppUnderTest.class);
}
//..
}
All libraries of the app under test can only (!) be found in libs/yourlibrary.jar and are referenced in Project->Properties->Java Build Path->Libraries
I have the following setup:
LibProjectWithActivity - an android library project which contains PreviewActivity
AppProject - usual Android Project which uses LibProjectWithActivity
AppProjectTest - an Android JUnit Test Project which is an ActivityInstumentationTestCase2
First of all: all basic dependencies are set up correctly, because I can perfectly launch my app in emulator, it shows PreviewActivity just ok, things work - no problems here.
But I fail to launch the test... Here's the setup:
public class PreviewActivityTest
extends ActivityInstrumentationTestCase2<PreviewActivity> {
public PreviewActivityTest() {
// note: if i put "com.the7art.libprojectwithactivity" instead it wont find it
// and will fail with "activity not found"
super("com.the7art.appproject", PreviewActivity.class);
}
public void testDummy() {
getActivity();
}
}
This throws a NullPointerException like this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.the7art.appproject/com.the7art.libprojectwithactivity.PreviewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
.......
Caused by: java.lang.NullPointerException
at com.the7art.libprojectwithactivity.PreviewActivity.onCreate(PreviewActivity.java:37)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
... 11 more
Here are lines PreviewActivity.java:36,37:
mButtonAdd = (Button) findViewById(R.id.button_add);
mButtonAdd.setOnClickListener(this);
So what this exception means is that R.id.button_add (which is in a libproject) is perfectly found, but the view by this id can't be found. If I comment out this code, it'll crash similarly when trying to obtain another resource.
Looks like test uses right R.java, but wrong Context object. Or something like this.
Again, the app code is ok, because it works in emulator, something is wrong with test setup...
Any hints on what is wrong and how to fix this?
Yes the test can be only use the Activity project as a target, look at the projectTarget in your Test manifest file.
This means that your test can use the same context as your application does. Accessing only the application's project resources. However, you can't access Library's project resources, you can use the R.string.XX from the library's project because this was first built it, and it's only a generated identifier that will be used in the application's context to find the resource. So the result will be an mistake in the resource found or a resource not found exception.
There are 2 ways to test a library project, here they are
I get the following stack trace when running my Android tests on the Emulator:
java.lang.NoClassDefFoundError: client.HttpHelper
at client.Helper.<init>(Helper.java:14)
at test.Tests.setUp(Tests.java:15)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:164)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:151)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:425)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1520)
Caused by: java.lang.IllegalAccessError: cross-loader access from pre-verified class
at dalvik.system.DexFile.defineClass(Native Method)
at dalvik.system.DexFile.loadClass(DexFile.java:193)
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:203)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
... 11 more
I run my tests from an extra project. And it seems there are some problems with loading the classes from the other project. I have run the tests before but now they are failing. The project under tests runs without problems.
Line 14 of the Helper Class is:
this.httpHelper = new HttpHelper(userProfile);
I start a HttpHelper class that is responsible for executing httpqueries. I think somehow this helper class is not available anymore, but I have no clue why.
I was having the same problem but in my case I was adding the library reference to both the application and test projects.
By removing the reference to library from the test project and leaving only the reference to the application project the problem was solved. The java.lang.IllegalAccessError exception and the message 'Class resolved by unexpected DEX' were the clues to help solve this problem.