title: Unable to spy on an Android class
body: <p>I have a simple android class of constants like this:</p>
  
  <pre><code>public class Consts extends BaseConstants {
  
      public static final String SCHEME = "http";
  
      // Tag used to cancel the request
      public static final String TAG_JSON = "json_obj_req";
  }
  </code></pre>
  
  <p>there is nothing else in it so it should be simple to mock.  I am calling this in my testcase:</p>
  
  <p><code>Mockito.spy(Consts.class);</code>  ...which is failing.  Below is the testcase file:</p>
  
  <pre><code>public class ApplicationTest extends ActivityInstrumentationTestCase2&lt;MainActivity&gt; {
  
    MainActivity mActivity;
  
    public ApplicationTest() {
        super(MainActivity.class);
    }
  
    @Override
    protected void setUp() throws Exception {
        super.setUp();
  
        setActivityInitialTouchMode(false);
  
        mActivity = getActivity();
    }
  
    public void testUrlValid() {
        Mockito.spy(Consts.class);
    }
  
  }
  </code></pre>
  
  <p>and here is the logcat output from the test case:</p>
  
  <pre><code>Running tests
  Test running started
  org.mockito.exceptions.base.MockitoException:
  Cannot mock/spy class java.lang.Class
  Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types
  </code></pre>
  
  <p>-------UPDATE:</p>
  
  <p>I want to spy on  my mainActivity class but i get the same Mockito exception: heres the class im testing:</p>
  
  <pre><code>import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  
  
  public class MainActivity extends ListPageActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                  .add(R.id.container, new SummaryFragment(),"SummaryFragment")
                  .commit();
        }
  
        loadBrandSound();
  
       if (!isNetworkAvailable())
            showToast(getString(R.string.no_network));
    }
  
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is   present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
  
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
  
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
  
        return super.onOptionsItemSelected(item);
    }
  }
  </code></pre>
  
  <p>and here is my simple test case:  </p>
  
  <pre><code>import android.test.ActivityInstrumentationTestCase2;
  import android.widget.Button;
  import android.widget.EditText;
  
  import org.mockito.Mockito;
  
  public class ApplicationTest extends ActivityInstrumentationTestCase2&lt;MainActivity&gt; {
  
      MainActivity mActivity;
      private Button goBtn;
      private EditText et_query;
      private RecyclerListAdapter mAdapter;
  
      public ApplicationTest() {
          super(MainActivity.class);
      }
  
      @Override
      protected void setUp() throws Exception {
          super.setUp();
  
          setActivityInitialTouchMode(false);
  
          mActivity = getActivity();
  
          goBtn=(Button)mActivity.findViewById(
                  R.id.btn_go);
          et_query = (EditText)mActivity.findViewById(R.id.et_query);
      }
  
      @Override
      protected void tearDown() throws Exception {
          super.tearDown();
      }
  
      public void testPreconditions() {
          assertTrue(mActivity.isNetworkAvailable());
          isLayoutValid();
      }
  
      public void isLayoutValid(){
          assertNotNull(goBtn);
      }
  
      public void testSomething(){
          Mockito.spy(MainActivity.class);
      }
  
  }
  </code></pre>
  
  <p>why is it failing on the spy line ? here is the log:</p>
  
  <pre><code>    Running tests
  Test running started
  org.mockito.exceptions.base.MockitoException:
  Cannot mock/spy class java.lang.Class
  Mockito cannot mock/spy following:
  - final classes
  - anonymous classes
  - primitive types
  at mypackage.ApplicationTest.testSomething(ApplicationTest.java:65)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
  at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
  at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)
  
  Finish
  </code></pre>