I have a simple android class of constants like this:
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"; } there is nothing else in it so it should be simple to mock. I am calling this in my testcase:
Mockito.spy(Consts.class); ...which is failing. Below is the testcase file:
public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> { 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); } } and here is the logcat output from the test case:
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 -------UPDATE:
I want to spy on my mainActivity class but i get the same Mockito exception: heres the class im testing:
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); } } and here is my simple test case:
import android.test.ActivityInstrumentationTestCase2; import android.widget.Button; import android.widget.EditText; import org.mockito.Mockito; public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> { 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); } } why is it failing on the spy line ? here is the log:
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