I'm having trouble importing some of the Android UI testing framework classes - I just can't figure out what is going wrong!
This is my class:
@RunWith(AndroidJUnit4.class) @LargeTest public class ExampleUnitTest { @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class); @Test public void listGoesOverTheFold() { onView(withText("Hello world!")).check(matches(isDisplayed())); } }
But for some reason I get the errors 'cannot find symbol ActivityTestRule' and 'cannot find symbol AndroidJUnit4'. I've tried to import them but they cannot be found.
The dependencies in build.gradle are set to:
compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' androidTestCompile 'com.android.support:support-annotations:23.4.0' androidTestCompile 'com.android.support.test:runner:0.4' androidTestCompile 'com.android.support.test:rules:0.4' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1' androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
So I think I have all the dependencies setup - I've been trying many things but with no luck.
Anyone have any ideas?
Earlier in the module's build.gradle file, we have a testInstrumentationRunner statement: testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" (from ToDoTests/build.gradle) This tells Android how to run our JUnit instrumented tests. JUnit uses “runner” classes for this role, and androidx.
The rule launches the chosen activity before each test annotated with @Test , as well as before any method annotated with @Before . The rule terminates the activity after the test completes and all methods annotated with @After finish.
public ActivityTestRule (Class<T> activityClass, boolean initialTouchMode) Similar to ActivityTestRule but defaults to launch the activity under test once per Test method. It is launched before the first Before method, and terminated after the last After method.
Add these in the newer version:
androidTestImplementation 'com.android.support.test:rules:1.0.2' androidTestImplementation 'com.android.support.test:runner:1.0.2'
There are two different types of tests you can set up in Android
Unit Tests
test/java
packagetestCompile
Instrumentation Tests
androidTest/java
packageandroidTestCompile
From what I can tell you are trying to write instrumentation tests with Espresso but have your test in the test/java
package which is for unit tests. In that case you need to move your test class to the androidTest/java
package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With