Why cannot I import AndroidJUnit4 and ActivityTest

2019-01-23 01:03发布

I'm having trouble importing some of the Android UI testing framework clases - 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 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?

6条回答
欢心
2楼-- · 2019-01-23 01:29

Add dependency.

androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'
查看更多
Summer. ? 凉城
3楼-- · 2019-01-23 01:44

need this add dependencies

 testCompile 'com.android.support.test:rules:0.5'
 testCompile 'com.android.support.test:runner:0.5'
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-23 01:50

Add these in the newer version:

androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
查看更多
狗以群分
5楼-- · 2019-01-23 01:54

There are two different types of tests you can set up in Android

Unit Tests

  • These run directly on the JVM and do not have access to the Android framework classes.
  • They are kept in the test/java package
  • Dependencies need to added in the build.gradle file with the command testCompile
  • You generally use Mockito, Robolectric & JUnit for these tests

Instrumentation Tests

  • These run on an Android emulator and have full access to all the Android classes
  • They are kept in the androidTest/java package
  • Dependencies need to be added to build.gradle with androidTestCompile
  • You generally use Espresso and JUnit for these tests

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.

查看更多
6楼-- · 2019-01-23 01:55

If you migrated to AndroidX, use this:

androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test:runner:1.1.1'
查看更多
来,给爷笑一个
7楼-- · 2019-01-23 01:56

Adding:

androidTestImplementation 'com.android.support.test:rules:1.0.2'

solves problem, but don't forget to sync the project with gradle files. Only then the changes will take effect.

查看更多
登录 后发表回答