title: Testing Activities in Xamarin.Android
body: <p>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).</p>
  
  <p>Android has some good <a href="http://developer.android.com/tools/testing/activity_testing.html" rel="nofollow">documentation</a> on the subject. However, I've not found anyone that has attempted to do this with Xamarin. Many of the <a href="http://developer.android.com/reference/android/test/package-summary.html" rel="nofollow">classes</a> in <code>android.test</code> are missing in Xamarin.</p>
  
  <p>So far I've attempted something like the following in the Xamarin test project that uses NUnit:</p>
  
  <pre><code>    [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);
          }
      }
  </code></pre>
  
  <p>Later on I'd have tests that use the activity.</p>
  
  <p>I also have to set the following in my manifest:</p>
  
  <pre><code>&lt;application android:label="My Tests"&gt;
      &lt;uses-library android:name="android.test.runner" /&gt;
  &lt;/application&gt;
  </code></pre>
  
  <p>The problem is, in my unit test above a <code>Java.Lang.NullPointerException</code> is thrown in my activity's <code>base.OnCreate()</code> method. I'm sure I'm missing something, because I'm in uncharted waters here.</p>
  
  <p>Here is the stack trace:</p>
  
  <pre><code>[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) &lt;IL 0x00017, 0x0001f&gt;
  [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)
  </code></pre>
  
  <p>Has anyone attempted this before?</p>