Hi
Im trying to create some jUnit tests for my Activities.
The main activity seems easy to test by extending ActivityInstrumentationTestCase2<Main>
and when i click "Run..." the app and the tests are installed on the emulator and runs fine. I can see that the main activity is starting.
The problem is that I can't figure out what I need to do to test my second activity. This activity is an activity that is normally started from within the Main activity, but I was hoping i could test it like a "stand-alone" activity. The problem is that i just keep getting
Test run failed: java.lang.NullPointerException
when i try to start the test.
// CODE THAT WORKS
package net.myapp.android;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.ImageView;
public class AboutActivityTest extends ActivityInstrumentationTestCase2<Main> {
private Main mActivity;
public AboutActivityTest() {
super("net.myapp.android", Main.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
}
public void testPreConditions() {
assertTrue(mActivity != null);
}
public void testSpinnerUI2() {
ImageView mImageView = (ImageView) mActivity.findViewById(net.myapp.android.R.id.b1);
assertTrue(mImageView != null);
}
}
// CODE THAT DOES NOT WORK
package net.myapp.android;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.ImageView;
public class AboutActivityTest extends ActivityInstrumentationTestCase2<SubActivity> {
private SubActivitym;
public AboutActivityTest() {
super("net.myapp.android", SubActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
}
public void testPreConditions() {
assertTrue(mActivity != null);
}
public void testSpinnerUI2() {
ImageView mImageView = (ImageView) mActivity.findViewById(net.myapp.android.R.id.b1);
assertTrue(mImageView != null);
}
}
The activities are defined in the AndroidManifest.xml like this:
<activity android:name="net.myapp.android.Main" android:label="@string/app_name"
android:launchMode="singleTop" android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- SubActivity -->
<activity android:name="net.myapp.android.SubActivity" android:label="@string/about"
So, the problem is when I'm trying to start the SubActivity test i get this error on the eclipse console:
[2010-09-13 14:01:58 - testAndroid] Collecting test information
[2010-09-13 14:02:00 - testAndroid] Sending test information to Eclipse
[2010-09-13 14:02:00 - testAndroid] Running tests...
[2010-09-13 14:02:03 - testAndroid] Test run failed: java.lang.NullPointerException
[2010-09-13 14:02:03 - testAndroid] Test run complete
Any ideas anyone?
UPDATE - RESOLVED:
It was an obvious mistake by me. The NullPointerException was coused by the onCreate method inside the SubActivity. The eclipse console didn't show that, I had to look at ddms/logcat.
I was trying to read out an extra parameter from the intent, but it was never set before startup. I had to use putExtra before i called getActivity :
Intent intent = new Intent();
intent.putExtra("ver","1.16");
setActivityIntent(intent);
mActivity = getActivity();