android - How to call onCreate of application class while running activity tests using Espresso -


in android app, have application class extends multidexapplication. let's call myapplicationclass in oncreate() of myapplicationclass.java, set static variables. in oncreate() method of activities variables using static methods.

public class myapplicationclass extends multidexapplication {    private static string value;    public static void setvalue(string value) {     myapplicationclass.value = value;   }    public static string getvalue() {     return myapplicationclass.value;   } } 

now using espresso framework, writing ui tests activity using following code

public class myactivitytest{    @rule   public activitytestrule activitytestrule =       new activitytestrule(myactivity.class);    @test   public void testbuttonisvisible() {      //some test code.   } } 

after running test android studio, oncreate() method of myactivity gets called , tries static variables. value of variables null. reason simple. oncreate() of myapplicationclass.java doesn't called in process.

so how call oncreate() method of application class before launching activity in espresso?

p.s. please don't advice regarding setting , getting of static variables. requirement of code.

i had same problem , spent lot of time until realized use custom testrunner , ovveride newapplication method. because of during tests used class application empry oncreate method. once switched default testrunner oncreate application executed expected.

@override @nonnull public application newapplication(@nonnull classloader cl,                                   @nonnull string classname,                                   @nonnull context context)         throws instantiationexception,         illegalaccessexception,         classnotfoundexception {      return super.newapplication(cl,testplayerapplication.class.getname(), context); } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -