Issue
I recently started looking at using the Robolectric gradle plugin to test one of my applications. The plugin itself is easy to setup in the project into the project, but I keep hitting the same error when trying to run a sample test. My code is as follows:
My Activity
package com.example.trial.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void emptyMethod(String empty) {
}
}
My Robolectric Test
package com.example.trial.myapplication;
import android.app.Application;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class ApplicationTest extends ApplicationTestCase<Application> {
private MyActivity myActivity;
public ApplicationTest() {
super(Application.class);
}
@Before
public void setUp() throws Exception {
myActivity = Robolectric.buildActivity(MyActivity.class).create().get();
}
@SmallTest
public void testSomething() throws Exception {
assertTrue(myActivity != null);
}
}
Running the test application throws the following error pointing at the code in the setup method.
org.fest.reflect.exception.ReflectionError: Unable to find method '$$robo$getData' in com.example.ronand.myapplication.MyActivity with parameter type(s) []
at org.fest.reflect.method.Invoker.lookupInClassHierarchy(Invoker.java:78)
at org.fest.reflect.method.Invoker.createInvoker(Invoker.java:60)
at org.fest.reflect.method.Invoker.newInvoker(Invoker.java:55)
at org.fest.reflect.method.MethodReturnType.in(MethodReturnType.java:66)
at org.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:434)
at org.robolectric.Robolectric.shadowOf_(Robolectric.java:1034)
at org.robolectric.util.ComponentController.<init>(ComponentController.java:36)
at org.robolectric.util.ActivityController.<init>(ActivityController.java:45)
at org.robolectric.util.ActivityController.<init>(ActivityController.java:41)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:1376)
at com.example.trial.myapplication.ApplicationTest.setUp(ApplicationTest.java:23)
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:1741)
I have come across a couple of questions which indicate that this error is related to the use of Robolectric.shadowOf()
however I do not use this anywhere in my code. If someone could point me in the right direction I would greatly appreciate it.
Thanks!
Solution
Not sure if that is the core issue, but you are mixing Robolectric with Android's testing framework. Robolectric runs tests on the JVM (it does not need nor use an emulator/device) and uses JUnit 4, not 3.
So you don't need to extend the ApplicationTestCase, nor that constructor, and the annotation should be @Test (which is from JUnit4), not @SmallTest (which is from Android testing). You'll know it's right if you don't have any more imports from android.test.*
Also make sure you run these as JUnit tests, not Android UI tests, if that option comes up in your IDE.
Answered By - Alex Florescu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.