Reputation: 26495
I'm wanting to unit test some interactions on activity lifecycle (I'm working on a data binding library, that can't have unit tests without it).
Android has some good documentation on the subject. However, I've not found anyone that has attempted to do this with Xamarin. Many of the classes in android.test
are missing in Xamarin.
So far I've attempted something like the following in the Xamarin test project that uses NUnit:
[TestFixture]
public class MyActivityTests
{
private TestActivity _activity;
private TestViewModel _viewModel;
private Instrumentation _instrumentation;
[SetUp]
public void SetUp()
{
_viewModel = new TestViewModel();
_activity = new TestActivity();
_instrumentation = new InstrumentationTestRunner();
_instrumentation.Start();
_instrumentation.CallActivityOnCreate(_activity, Bundle.Empty);
}
}
Later on I'd have tests that use the activity.
I also have to set the following in my manifest:
<application android:label="My Tests">
<uses-library android:name="android.test.runner" />
</application>
The problem is, in my unit test above a Java.Lang.NullPointerException
is thrown in my activity's base.OnCreate()
method. I'm sure I'm missing something, because I'm in uncharted waters here.
Here is the stack trace:
[MonoDroid] UNHANDLED EXCEPTION: Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
[MonoDroid] at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/35376881/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:895
[MonoDroid] at Android.App.Activity.OnCreate (Android.OS.Bundle) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/35376881/source/monodroid/src/Mono.Android/platforms/android-18/src/generated/Android.App.Activity.cs:2193
[MonoDroid] at XamBind.Droid.Tests.TestActivity.OnCreate (Android.OS.Bundle) [0x00003] in /Users/jonathanpeppers/Desktop/MonoTouch/XamBind/XamBind.Droid.Tests/Activities/TestActivity.cs:19
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.10.2-branch/35376881/source/monodroid/src/Mono.Android/platforms/android-18/src/generated/Android.App.Activity.cs:2178
[MonoDroid] at (wrapper dynamic-method) object.640629b6-d042-45f3-840b-28e31832a912 (intptr,intptr,intptr) <IL 0x00017, 0x0001f>
[MonoDroid]
[MonoDroid] --- End of managed exception stack trace ---
[MonoDroid] java.lang.NullPointerException
[MonoDroid] at android.app.Activity.onCreate(Activity.java:884)
[MonoDroid] at xambind.droid.tests.TestActivity.n_onCreate(Native Method)
[MonoDroid] at xambind.droid.tests.TestActivity.onCreate(TestActivity.java:28)
[MonoDroid] at android.app.Activity.performCreate(Activity.java:5133)
[MonoDroid] at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
[MonoDroid] at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
[MonoDroid] at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
[MonoDroid] at android.view.View.performClick(View.java:4240)
[MonoDroid] at android.view.View$PerformClick.run(View.java:17721)
[MonoDroid] at android.os.Handler.handleCallback(Handler.java:730)
[MonoDroid] at android.os.Handler.dispatchMessage(Handler.java:92)
[MonoDroid] at android.os.Looper.loop(Looper.java:137)
[MonoDroid] at android.app.ActivityThread.main(ActivityThread.java:5103)
[MonoDroid] at java.lang.reflect.Method.invokeNative(Native Method)
[MonoDroid] at java.lang.reflect.Method.invoke(Method.java:525)
[MonoDroid] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
[MonoDroid] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
[MonoDroid] at dalvik.system.NativeStart.main(Native Method)
Has anyone attempted this before?
Upvotes: 3
Views: 2163
Reputation: 26495
I think this isn't supported yet with Xamarin.Android: https://bugzilla.xamarin.com/show_bug.cgi?id=6043
Because this stuff depends on JUnit, they can't bind the classes to C#. Hopefully they get it working and its coming.
What I did for my situation was to rewrite my library so you don't have to subclass Activity
, then I could write my tests no problem.
Upvotes: 2