title: Fastest way to create a mock Activity for testing
body: <p>I'm currently unit-testing a library. In certain classes, I need to pass an activity as parameter in some static methods. The library itself contains no activities. I need to somehow obtain an instance of a mock activity to use in each individual method test. </p>
  
  <p>I've already read the <a href="http://developer.android.com/tools/testing/activity_testing.html" rel="noreferrer">Activity Testing Tutorial</a> and the <a href="http://developer.android.com/tools/testing/testing_android.html" rel="noreferrer">Testing Fundamentals</a> section. Most of what it is said there only makes sense if you are about to test Activities already existing in the project to be tested. But I just need a mock one to do things like showing dialogs and run short tasks in the Ui thread.</p>
  
  <p>What would the fastest, simplest way to achieve this? Should I create the mock activity in my test project and also provide xml layout resources for the dummy UI? </p>
  
  <hr>
  
  <p>UPDATE<br>
  Since I've not found any way of creating a mock activity automatically I decided to provide it by myself. I've created inside the test project a dummy activity doing nothing and provided a dummy layout via xml. Then I coded my test extending <code>ActivityInstrumentationTestCase2</code>:<br></p>
  
  <pre><code>    public class LibraryTest extends ActivityInstrumentationTestCase2&lt;MockActivity&gt; {
  
          public LibraryTest(String name) {
              super(MockActivity.class);
          }
  
          protected void setUp() throws Exception {
              super.setUp();
          }
  
          public void testAMethodFromLibrary() {
              fail("Not yet implemented");
          }
      }
  </code></pre>
  
  <p>Where <code>MockActivity</code> is the aforementioned mock activity I've created in this test project. However, seems that the Android test framework is having trouble launching the activity and it comes up with this exception:</p>
  
  <pre><code>        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:520)
          at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
          Caused by: java.lang.NullPointerException: Method name must not be null.
          at java.lang.ClassCache.findMethodByName(ClassCache.java:297)
          at java.lang.Class.getMethod(Class.java:985)
          at android.test.suitebuilder.TestMethod.getAnnotation(TestMethod.java:60)
          at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:39)
          at android.test.suitebuilder.annotation.HasMethodAnnotation.apply(HasMethodAnnotation.java:30)
          at com.android.internal.util.Predicates$OrPredicate.apply(Predicates.java:106)
          at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:42)
          at android.test.suitebuilder.annotation.HasAnnotation.apply(HasAnnotation.java:31)
          at com.android.internal.util.Predicates$NotPredicate.apply(Predicates.java:122)
          at android.test.suitebuilder.TestSuiteBuilder.satisfiesAllPredicates(TestSuiteBuilder.java:254)
          at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:190)
          at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:373)
          at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4218)
          at android.app.ActivityThread.access$3000(ActivityThread.java:125)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:123)
          at android.app.ActivityThread.main(ActivityThread.java:4627)
          at java.lang.reflect.Method.invokeNative(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
          at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Now I'm totally lost here. How can this be so complicated? Have I chosen the correct way? I just want to launch a dialog in a test method. Maybe the framework is messing up because the activity to test is not in the target project?</p>
  
  <p>Any help here would be much appreciated. I'm getting out of time and if I can't find the proper way to do this I'd have to create a second project using my library, move the mock activity there and test from the test project. This is a lot of code because I'd have to include a method in a (now not generic) mock activity just to call every library method I want to test.</p>