java - Is it possible to run Android Espresso unit tests in @BeforeClass annotated methods? -


i have issue using junit4 @beforeclass annotation in instrumented android unit test (i'm using espresso gui testing library). add test @beforeclass annotation, android studio 1.5.1 not run tests @ instead prints "empty test suite". i'm not using test suite. searched site , web couldn't find solution. thought might problem code, called within @beforeclass method fails (tdd), error occurs when code, working in normal test cases, put in @beforeclass annotated method.

thank you.

update: after checking logcat output, 1 commenter suggested, seems problem problem no activity started: no activities found. did forget launch activity calling getactivity() or startactivitysync or similar?

how should this? can't use activitytestrule field, @beforeclass annotated method static.

maybe i'm using @beforeclass annotation in wrong way. impression can use annotation execute tests before other tests in test class. looking replacement testng annotation "dependsonmethods" here. maybe i'm better off using @fixmethodorder(methodsorters.name_ascending) annotation on test class , renaming first test case aaa_my_testcase.

could please comment on that? thanks.

rephrased title of question.

import android.support.test.rule.activitytestrule; import android.support.test.runner.androidjunit4;  import org.junit.beforeclass; import org.junit.fixmethodorder; import org.junit.rule; import org.junit.test; import org.junit.runner.runwith; import org.junit.runners.methodsorters;  import static android.support.test.espresso.espresso.onview; import static android.support.test.espresso.espresso.pressback; import static android.support.test.espresso.action.viewactions.click; import static android.support.test.espresso.assertion.viewassertions.matches; import static android.support.test.espresso.matcher.viewmatchers.isdisplayed; import static android.support.test.espresso.matcher.viewmatchers.isenabled; import static android.support.test.espresso.matcher.viewmatchers.withcontentdescription; import static android.support.test.espresso.matcher.viewmatchers.withid; import static android.support.test.espresso.matcher.viewmatchers.withtext; import static org.hamcrest.corematchers.allof; import static org.hamcrest.corematchers.not;   @runwith(androidjunit4.class) public class mainactivitytest {      @rule     public activitytestrule<mainactivity> menuactivitytestrule = new activitytestrule<>(mainactivity.class);      private static void checksignbrowserisdisplayed() {         onview(withtext(r.string.sign_browser)).check(matches(isdisplayed()));     }      @beforeclass     public static void checksignbrowserisdisplayedonappstartup() {         checksignbrowserisdisplayed();     } 

build.app:

apply plugin: 'com.android.application'  android {     compilesdkversion 23     buildtoolsversion "23.0.2"      defaultconfig {         applicationid "foo"         minsdkversion 15         targetsdkversion 23         versioncode 1         versionname "1.0"         testinstrumentationrunner "android.support.test.runner.androidjunitrunner"     }     buildtypes {         release {             minifyenabled false             proguardfiles getdefaultproguardfile('proguard-android.txt'), 'proguard-rules.pro'         }         debug {             debuggable true         }     }     testoptions {         unittests.returndefaultvalues = true     } }  dependencies {     testcompile 'org.mockito:mockito-core:1.10.19'     testcompile 'junit:junit:4.12'     androidtestcompile 'junit:junit:4.12'     androidtestcompile 'com.android.support:support-annotations:23.1.1'     androidtestcompile 'com.android.support.test:runner:0.4.1'     androidtestcompile 'com.android.support.test:rules:0.4.1'     androidtestcompile 'org.hamcrest:hamcrest-library:1.3'     androidtestcompile 'com.android.support.test.espresso:espresso-core:2.2.1'     // required if want use mockito android instrumentation tests - not needed now.     // androidtestcompile 'org.mockito:mockito-core:1.+'     // androidtestcompile "com.google.dexmaker:dexmaker:1.2"     // androidtestcompile "com.google.dexmaker:dexmaker-mockito:1.2"     compile filetree(include: ['*.jar'], dir: 'libs')     compile 'org.apache.commons:commons-lang3:3.4'     compile 'com.android.support:appcompat-v7:23.1.1'     compile 'com.android.support:design:23.1.1' } 

test output:

running tests

test running startedfinish

empty test suite.

logcat output:

01-25 10:22:42.746 22098-22118/foo i/testrunner: run started: 5 tests 01-25 10:22:42.764 22098-22118/foo d/inputmanagereventinjectionstrategy: creating injection strategy input manager. 01-25 10:22:42.890 22098-22118/foo i/testrunner: failed: foo.mainactivitytest 01-25 10:22:42.890 22098-22118/foo i/testrunner: ----- begin exception ----- 01-25 10:22:42.891 22098-22118/foo i/testrunner: java.lang.runtimeexception: no activities found. did forget launch activity calling getactivity() or startactivitysync or similar?                                                                                                    @ android.support.test.espresso.base.rootviewpicker.waitforatleastoneactivitytoberesumed(rootviewpicker.java:189)                                                                                                    @ android.support.test.espresso.base.rootviewpicker.findroot(rootviewpicker.java:134)                                                                                                    @ android.support.test.espresso.base.rootviewpicker.get(rootviewpicker.java:80)                                                                                                    @ android.support.test.espresso.viewinteractionmodule.providerootview(viewinteractionmodule.java:69)                                                                                                    @ android.support.test.espresso.viewinteractionmodule_providerootviewfactory.get(viewinteractionmodule_providerootviewfactory.java:23)                                                                                                    @ android.support.test.espresso.viewinteractionmodule_providerootviewfactory.get(viewinteractionmodule_providerootviewfactory.java:9)                                                                                                    @ android.support.test.espresso.base.viewfinderimpl.getview(viewfinderimpl.java:68)                                                                                                    @ android.support.test.espresso.viewinteraction$2.run(viewinteraction.java:166)                                                                                                    @ java.util.concurrent.executors$runnableadapter.call(executors.java:422)                                                                                                    @ java.util.concurrent.futuretask.run(futuretask.java:237)                                                                                                    @ android.os.handler.handlecallback(handler.java:739)                                                                                                    @ android.os.handler.dispatchmessage(handler.java:95)                                                                                                    @ android.os.looper.loop(looper.java:135)                                                                                                    @ android.app.activitythread.main(activitythread.java:5312)                                                                                                    @ java.lang.reflect.method.invoke(native method)                                                                                                    @ java.lang.reflect.method.invoke(method.java:372)                                                                                                    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:901)                                                                                                    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:696) 01-25 10:22:42.891 22098-22118/foo i/testrunner: ----- end exception ----- 01-25 10:22:42.891 22098-22118/foo i/testrunner: failed: test mechanism 01-25 10:22:42.892 22098-22118/foo i/testrunner: ----- begin exception ----- 01-25 10:22:42.892 22098-22118/foo i/testrunner: java.lang.nullpointerexception: attempt invoke virtual method 'void android.os.bundle.putstring(java.lang.string, java.lang.string)' on null object reference                                                                                                    @ android.support.test.internal.runner.listener.instrumentationresultprinter.reportfailure(instrumentationresultprinter.java:183)                                                                                                    @ android.support.test.internal.runner.listener.instrumentationresultprinter.testfailure(instrumentationresultprinter.java:173)                                                                                                    @ org.junit.runner.notification.synchronizedrunlistener.testfailure(synchronizedrunlistener.java:63)                                                                                                    @ org.junit.runner.notification.runnotifier$4.notifylistener(runnotifier.java:142)                                                                                                    @ org.junit.runner.notification.runnotifier$safenotifier.run(runnotifier.java:72)                                                                                                    @ org.junit.runner.notification.runnotifier.firetestfailures(runnotifier.java:138)                                                                                                    @ org.junit.runner.notification.runnotifier.firetestfailure(runnotifier.java:132)                                                                                                    @ org.junit.internal.runners.model.eachtestnotifier.addfailure(eachtestnotifier.java:23)                                                                                                    @ org.junit.runners.parentrunner.run(parentrunner.java:369)                                                                                                    @ org.junit.runners.suite.runchild(suite.java:128)                                                                                                    @ org.junit.runners.suite.runchild(suite.java:27)                                                                                                    @ org.junit.runners.parentrunner$3.run(parentrunner.java:290)                                                                                                    @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71)                                                                                                    @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288)                                                                                                    @ org.junit.runners.parentrunner.access$000(parentrunner.java:58)                                                                                                    @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268)                                                                                                    @ org.junit.runners.parentrunner.run(parentrunner.java:363)                                                                                                    @ org.junit.runner.junitcore.run(junitcore.java:137)                                                                                                    @ org.junit.runner.junitcore.run(junitcore.java:115)                                                                                                    @ android.support.test.internal.runner.testexecutor.execute(testexecutor.java:54)                                                                                                    @ android.support.test.runner.androidjunitrunner.onstart(androidjunitrunner.java:240)                                                                                                    @ android.app.instrumentation$instrumentationthread.run(instrumentation.java:1928) 

i had same issue , because of rule, can set activity launch in rule's constructor:

@rule public activitytestrule<mainactivity> menuactivitytestrule =          new activitytestrule<>(mainactivity.class, true, true); 

last argument responsible launching activity.


Comments