mardi 28 juillet 2020

Field value set using reflection from listner class is set to null when corresponding test class is executed

I am working on testng java and trying to set value of a test class field using reflection from listner class. The value is set and retrieved successfully from listner class but produces null when utilizing the same variable field in @test method

@Listners(Testrunner.class)
public class testclass{
    String strvalue;
   
    @Test
    public void m1(){
      Sysout(strvalue)------> produces null when testng runs @test method but not testvalue
    }
}

Testrunner implements ITestListener{

  @Override
    public void onTestStart(ITestContext arg0) {
        testclass cls = new testclass();
        Field f1 =  testclass.class.getDeclaredField("strvalue");
        f1.setaccessible(true);
        f1.set(cls,"testvalue");
        Sysout(f1.get(cls));---------> i am able to get the value s "testvalue"
        
    }
}

Am i missing any logic in this code, help me in complete this




Aucun commentaire:

Enregistrer un commentaire