Reputation: 1952
I am new to Instrumentation Testing on Android and was learning from the sample at https://github.com/googlesamples/android-testing/tree/master/runner/AndroidJunitRunnerSample to use AndroidJUnit4.
That code works perfectly.
Trying to implement the same login on my project, however, I get the following exception:
Running tests
Test running started
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:38)
at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:36)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.buildAndroidRunner(AndroidAnnotatedBuilder.java:57)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:45)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:81)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:791)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:754)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:341)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:238)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
My AndroidTestSuite looks like:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({InstrumentationTestSuite.class})
public class AndroidTestSuite {}
And my InstrumentationTestSuite looks like:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({LoginInstrumentationTest.class})
public class InstrumentationTestSuite {}
My LoginInstrumentationTest is
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginInstrumentationTest {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(
LoginActivity.class);
@Test
public void attemptToLogin() {
login("[email protected]", "12345678");
}
private void login(String email, String password) {
// Enter user email
onView(withId(R.id.email)).perform(typeText(email),
closeSoftKeyboard());
// Enter user password
onView(withId(R.id.password)).perform(typeText(password),
closeSoftKeyboard());
// Click on the login button
onView(withId(R.id.email_sign_in_button)).perform(click());
// Testing
onView(withId(R.id.email)).check(matches(withText(email)));
}
}
From what I search, usually the error I'm getting is due to a missing @Test
before my method.
The difference I noticed between my code and the one I'm learning from is the gradle version, which I use 1.5.0 and they use 2.0.0-beta2.
I also found that my Instrumentation.java that is called during the test has several "Cannot resolve symbol" error with I can't find the reason, since I use the same dependencies as the sample code.
Any one know what could be the reason of the No runnable methods
exception and how to solve it?
Upvotes: 1
Views: 1954
Reputation: 1952
I finally solved the problem. The problem was that I was planning on running unit tests also, not just instrumentation tests.
I had a class UnitTestSuite like:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({LoginUnitTest.class})
public class UnitTestSuite {}
And LoginUnitTest didn't implement any tests.
For some reason, even though UnitTestSuite wasn't called from AndroidTestSuite, it tried to run and returned No runnable methods
, since LoginUnitTest had no tests implemented. I've added unit tests and now the tests work perfectly.
Upvotes: 2