Issue
I am trying to run UI testing on an android application with espresso but when I run the test on a mobile device or an emulator the application opens and suddenly it closes itself way to fast, even before the test is ran.
It is not that the application is crashing, it just closes as soon as it opens only when a test is ran. I am not sure what I am missing in the test configuration that provokes this behavior.
this is my gradle file
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
defaultConfig {
applicationId "com.test1"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'jp.wasabeef:picasso-transformations:2.1.2'
compile 'com.google.firebase:firebase-database:9.4.0'
compile 'com.google.firebase:firebase-auth:9.4.0'
compile 'com.google.firebase:firebase-messaging:9.4.0'
compile 'com.google.android.gms:play-services-location:9.4.0'
compile 'com.google.android.gms:play-services-places:9.4.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
androidTestCompile ('com.android.support.test:runner:1.0.1') {
exclude module: 'support-annotations'
}
androidTestCompile ('com.android.support.test:rules:1.0.1') {
exclude module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-intents:3.0.1') {
exclude module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-web:3.0.1') {
exclude module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-core:3.0.1') {
exclude module: 'support-annotations'
}
}
apply plugin: 'com.google.gms.google-services'
this is my test
@RunWith(AndroidJUnit4.class)
public class PAC4Testing {
@Rule
public ActivityTestRule<BookListActivity> mActivityRule = new ActivityTestRule<>(
BookListActivity.class);
@Test
public void checkLoadedList() {
if (getRVcount() > 0){
assertEquals(9,getRVcount());
}
}
private int getRVcount(){
RecyclerView recyclerView = (RecyclerView) mActivityRule.getActivity().findViewById(R.id.book_list);
return recyclerView.getAdapter().getItemCount();
}
@Test
public void checkMenuOpened() {
}
@Test
public void checkLoadedWebViewFromDetail() {
}
}
Thanks!
EDIT these are logs, there is an error when the test is ran because the application closes, the test cant find anything on the screen.
java.lang.NullPointerException: Attempt to invoke virtual method 'int
android.support.v7.widget.RecyclerView$Adapter.getItemCount()' on a null object reference
at PAC4Testing.getRVcount(PAC4Testing.java:34)
at PAC4Testing.checkLoadedList(PAC4Testing.java:27)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:433)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)
Solution
The error clearly states that the recyclerView.getAdapter()
call in getRVcount()
is returning null
. And that is because even though you do get the view from RecyclerView recyclerView = (RecyclerView) mActivityRule.getActivity().findViewById(R.id.book_list);
the recyclerview that you get isn't the same as in the class since it is a fresh one and as no Adapters or LayoutManagers associated with it. As a result you can't call getItemCount()
on the adapter.
I am not sure what exactly are you trying to test here since you haven't posted the class' code but here are a couple of links you might want to take a look at and see if they are of any help to you https://spin.atomicobject.com/2016/04/15/espresso-testing-recyclerviews/ and http://alexander-thiele.blogspot.in/2016/01/espresso-ui-tests-and-recyclerview.html
Answered By - LeoNeo
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.