title: Android ApplicationTestCase using a MockContext
body: <p>I'm new to Android testing and I'm trying to create an ApplicationTestCase using a MockContext (well actually I'm trying to use a Renaming Mock Context). But I keep getting an AssertionFailedError. Here's my very basic code so far:</p>
  
  <p>AppTests.java</p>
  
  <pre><code>package com.myProject.test;
  
  import android.test.ApplicationTestCase;
  
  public class AppTests extends ApplicationTestCase&lt;MyApplication&gt; {
      public AppTests() {
          super(MyApplication.class);
      }
  
      @Override
      protected void setUp() throws Exception {
          final RenamingMockContext mockContext = new RenamingMockContext(getContext());
          setContext(mockContext);
          createApplication();
      }
  
  }
  </code></pre>
  
  <p>MyApplication.java</p>
  
  <pre><code>package com.myProject.test;
  
  import android.app.Application;
  
  public class MyApplication extends Application {
  
      public MyApplication() {
      }
  
      @Override
      public void onCreate() {
          super.onCreate();
      }
  
  }
  </code></pre>
  
  <p>RenamingMockContext.java</p>
  
  <pre><code>package com.myProject.test;
  
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.test.RenamingDelegatingContext;
  import android.test.mock.MockContext;
  
  public class RenamingMockContext extends RenamingDelegatingContext {
  
      private static final String PREFIX = "test.";
  
      public RenamingMockContext(Context context) {
          super (new DelegatedMockContext(context), PREFIX);
          //super(context, PREFIX);
      }
  
      private static class DelegatedMockContext extends MockContext {
          private Context mDelegatedContext;
          public DelegatedMockContext(Context context) {
              mDelegatedContext = context;
          }
  
          @Override
          public String getPackageName() {
              return mDelegatedContext.getPackageName();
          }
  
          @Override
          public SharedPreferences getSharedPreferences(
            String name, int mode) {
            return mDelegatedContext.getSharedPreferences(
              PREFIX + name, mode);
          }
  
      }
  
  }
  </code></pre>
  
  <p>Failure Trace:</p>
  
  <pre><code>junit.framework.AssertionFailedError
  at android.test.ApplicationTestCase.setupApplication(ApplicationTestCase.java:102)
  at android.test.ApplicationTestCase.createApplication(ApplicationTestCase.java:118)
  at com.myApplication.test.AppTests.setUp(AppTests.java:14)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1710)
  </code></pre>
  
  <p>Note if I use the second super() call in the RenamingMockContext constructor that is commented out (so don't use the  extended MockContext class) it works fine. </p>
  
  <p>Here is a reference <a href="https://stackoverflow.com/questions/5267671/unsupportedoperationexception-while-calling-getsharedpreferences-from-unit-tes">UnsupportedOperationException while calling getSharedPreferences() from unit test</a> which didn't work for me, and I also read through the book Android Application Testing Guide which gives an example exactly like this, but when I downloaded the source and ran it directly it gave the same error. </p>