Robotium test cannot find activity class - android

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

Related

Android Easy Mock test Error

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

Android to Unable to instantiate Application java.lang.ClassNotFoundException:

I am working on application which is host on android market. Sometimes (once a month ) I got a crash report:
Unable to instantiate application java.lang.ClassNotFoundException
App downloads are between 10,000-50,000. I don't know why this exceptions raise on some devices not all ( I tested it on 3 different devices & I couldn't re-produce it at my end).
I read articles/suggestions on different android forums regarding the issue but I didn't succeed in solving it. Does anyone face similar issue & suggest me what should I do?
Note:
I am extending application class like this
public class MyApplication extends Application {
}
I register it in the manifest.xml like this
<application android:icon="#drawable/app_icon"
android:label="#string/my_app_name" android:name="MyApplication">
Stack Trace :
java.lang.RuntimeException: Unable to instantiate application com.xyz.MyApplication java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk]
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:650)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4238)
at android.app.ActivityThread.access$3000(ActivityThread.java:126)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2076)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4633)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.xyz.MyApplication in loader dalvik.system.PathClassLoader[/mnt/asec/com.xyz-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newApplication(Instrumentation.java:942)
at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:645)
I don't know why application crash on some devices not all.
Some other similar questions indicate that this can be a user error. "/mnt/asec/..." indicates that the app is running from the sdcard. If the sdcard is removed it could cause this error. 3rd party apps or rooted devices can probably move an app to the sdcard even if its not allowed by the manifest.
Similar Question
I think the problem is with getApplication() which I have used in 10 different place. So I have used singleton pattern to solve this.
public class MyApplication extends Application {
private static MyApplication me;
#Override
public void onCreate() {
super.onCreate();
me = this ;
}
public static MyApplication getInstance() {
return me;
}
}
Now I have used getApplication() like this
MyApplication application = MyApplication.getInstance();
insted of
MyApplication application = (MyApplication) getApplication();
I have uploaded the fixed version on the market & now waiting if there is anymore this kind of crash. If everything goes perfect ( if no more crash in 2 weeks) then I will close the question. In meanwhile anyone has better idea or know the solution , please share it. Thanks,
In my case, I was compiling and signing with Eclipse ADT (with File > Export > Export Android Application...) but missing some classes if I decompile my .apk.
To solve it I use "Export an unsigned APK" and sign it using jarsigner and zipalign.
android:name=".MyApplication"
also consider adding full package

FragmentActivity Junit Testing

Me used Fragment of Android Compatibility Package, using the android-support-v4.jar. But I can't do the JUnit test on this.
My main FragmentActivity class is declared as follows
public class MyActivityClass extends FragmentActivity{
...............
}
Then in my test project
public class MyActivityClassTest extends ActivityInstrumentationTestCase2<MyActivityClass> {
public MyActivityClassTest() {
super("com.android.myproject", MyActivityClass.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
...................
}
public void testPreconditions() {
.................
}
public void testNotNull(){
................
}
}
But when I run as Android JUnit Test produce FailedToCreateTests[Runner:Junit3]
Failure Trace
java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.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:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
at com.android.myproject.test.MyActivityClassTest.<init>(MyActivityClassTest.java:28)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:336)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3982)
at android.app.ActivityThread.access$2900(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass
... 19 more
When I changed MyActivityClass to extends Activity it worked fine (MyActivityClass extends Activity)
Me used the same android-support-v4.jar in my both test and main project
ActivityInstrumentationTestCase2 is compatible with Fragments. You should only follow the steps mentioned in Android Testing: External libraries, which has been updated to cover the case of the android-support-v4.jar too.
Then, you will be able to write tests like this one:
public void testFragmentManager() {
FragmentActivity activity = getActivity();
assertNotNull(activity.getSupportFragmentManager());
}
or whatever you need to test.
I found a solution
The problem was
Caused by: java.lang.NoClassDefFoundError: com.android.myproject.MyActivityClass
It cannot find the class path even though I refer the same jar in both projects ( I also tried by using separate jar for both the project )
Now, I created my testing environment in the same project, then it worked
In my AndroidManifest.xml
<manifest...>
<!-- For doing JUnit test, Instrumentation Start (remove later) -->
<instrumentation
android:targetPackage="com.pe.android.isccasinos"
android:name="android.test.InstrumentationTestRunner" />
<!-- For doing JUnit test, Instrumentation End (remove later) -->
<application ...>
................
<!-- For doing JUnit test, add library starting (remove later) -->
<uses-library
android:name="android.test.runner" />
<!-- For doing JUnit test, add library ending (remove later) -->
</application>
<manifest>
Then I added my Testing class in my special package for testing
extends ActivityInstrumentationTestCase2<fragmentActivity>
Now everything is working fine :)
See this question for a better answer:
FragmentActivity can not be tested via ActivityInstrumentationTestCase2
You need to export the reference to the compatibility library from your app.
This is late, but I solve the same issue by only adding the android-support-v4.jar dependency to the main project, and removing it from the test project.
Then, make sure the main project is on the build path of the test project.
Do the same for any other libraries the main, and test projects might share.
My guess is that ActivityInstrumentationTestCase2 is not compatible with Fragments.
You should try something like Robolectric.
I have tried almost a day to deal with this issue and found a solution from another post
Basically your android-support-v4.jar (in my case) is used in the Application should be same with what you have used in your Test application. The best way to do is, remove the same jar from the libs directory of your test project and export the same from your Application project. That way you can get rid of this issue for sure.

ActivityInstrumentationTestCase2 and android library project: resources fail to load

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

Why do I get a Illegal Access Error when running my Android tests?

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.

Categories

Resources