java - Is there any way to use an instance variable in another method? -
java - Is there any way to use an instance variable in another method? -
lets have method defines , assigns value variable. want somehow able utilize variable , value within method.
i don't want pass variable argument because i'm running selenium test, , there multiple test methods depends on 1 method - means execute if 1 of test methods (that depends on it) executed.
i've tried using accessors/mutators assign id class variable, doesn't seem work
e.g
string mainid; public void setid(string s) { mainid = s; } public string getid() { homecoming mainid; } @test public void dosomething() { string numeric = this.randomnumeric(); string id = "d1234" + numeric; this.setid(id); ... // id number } @test (dependsonmethod = {"dosomething"}) public void somethinga() { ...sendkeys(this.getid()); // id - e.g search database see if id added correctly } @test (dependsonmethod = {"dosomething"}) public void somethingb() { ... // else id }
for sharing logic/variables between @test
methods, can either utilize instance method annotated @before
, invoked 1 time before every @test
method, or static method annotated @beforeclass
, invoked 1 time entire test class before @test
methods run.
in scenario, assuming need generate mainid
value 1 time , reuse same value across multiple @test
methods, need utilize @beforeclass
, store value static variable, so:
private static string mainid; @beforeclass public static void init() { //must public static void string numeric = randomnumeric(); mainid = "d1234" + numeric; } @test public void somethinga() { //use mainid (note belongs class, not instance) //... } //other tests...
note changing mainid
, dosomething
logic static requires alter randomnumeric
method static well.
java junit instance-variables
Comments
Post a Comment