Issue
When trying to run Android Espresso tests, the junit runner is unable to instantiate/find the class that you are testing. This can be resolved by updating your build.gradle file, but why is this required? Thank you for any insight!
For example, the following line will create an exception like the one below.
junit test:
@Rule
public ActivityTestRule<ChurchesActivity> mChurchesActivityTestRule =
new ActivityTestRule<>(ChurchesActivity.class);
Exception generated when running test:
junit.framework.AssertionFailedError: Exception in constructor: toolbarHasApplicationName (java.lang.NoClassDefFoundError: es.unizar.vv.mobile.catmdedit.app.view.ChurchesActivity
This issue is resolved by updating your build.gradle to exclude particular groups/modules (see below), but why is this required?
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
}
This question is a follow up to the following question:
Android Tests Exception in Constructor NoClassDefFoundError
Solution
It's needed because when using Gradle you may sometimes have version conflicts with transitive dependencies (dependencies included by the library you want to use). You can read more about it here.
So for instance, the espresso-contrib lib may have a different version of the recyclerview-v7 library than the one referenced in your application's dependencies. So you can either exclude it or specifically state which version of the library you want to use with the following:
ext {
supportLibraryVersion = '23.1.1'
}
configurations.all {
resolutionStrategy.force "com.android.support:support-annotations:$supportLibraryVersion"
resolutionStrategy.force "com.android.support:recyclerview-v7:$supportLibraryVersion"
resolutionStrategy.force "com.android.support:support-v4:$supportLibraryVersion"
}
Answered By - brwngrldev
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.