2
votes

i am currently Working on TestNG framework and facing the problem that -

I have declared a method to run before every test in class by declaring a method like

@BeforeMethod
@Parameters({"type"})
public void beforeMethod(@Optional("Krishna") String type)
{
    System.out.println("Type in Before Method is="+type);
}

here i am getting the value type inside this method is NULL.

But if i am using other testNG annotation like @BeforeSuite , @BeforeTest, @BeforeClass are working fine but @Optional annotation not passing default for @BeforeMethod and @AfterMethod annotation.

My complete Test Class is :

public class BeforeMethodTest 
{
@BeforeSuite
@Parameters({"type"})
public void beforeSuite(@Optional("Krishna") String type)
{
    System.out.println("Type in Before suite is="+type);
}

@BeforeTest
@Parameters({"type"})
public void beforeTest(@Optional("Krishna") String type)
{
    System.out.println("Type in Before Test is="+type);
}

@BeforeClass
@Parameters({"type"})
public void beforeClass(@Optional("Krishna") String type)
{
    System.out.println("Type in Before class is="+type);
}


@BeforeMethod
@Parameters({"type"})
public void beforeMethod(@Optional("Krishna") String type)
{
    System.out.println("Type in Before Method is="+type);
}

@Test
public void test()
{
    System.out.println("Test methods");
}

@AfterSuite
@Parameters({"type"})
public void afterSuite(@Optional("Krishna") String type)
{
    System.out.println("Type in After suite is="+type);
}

@AfterTest
@Parameters({"type"})
public void afterTest(@Optional("Krishna") String type)
{
    System.out.println("Type in After Test is="+type);
}

@AfterClass
@Parameters({"type"})
public void afterClass(@Optional("Krishna") String type)
{
    System.out.println("Type in After class is="+type);
}
@AfterMethod
@Parameters({"type"})
public void afterMethod(@Optional("Krishna") String type)
{
    System.out.println("Type in After Method is="+type);
}
}

and output is

Type in Before suite is=Krishna
Type in Before Test is=Krishna
Type in Before class is=Krishna
Type in Before Method is=
Test methods
Type in After Method is=
Type in After class is=Krishna
Type in After Test is=Krishna

i have executed this class by right click on class then selected run as testNG.

Please help me in identifying why null is passed in aftertest and beforetest

1
I do not think null was passed there, most probably an empty String (""). Which version of TestNG do you use? My guess would be: as your test() method has no parameters, so some kind of default was used for BeforeTest and AfterTest as a parameter, so Optional was not applied.Gábor Bakos

1 Answers

1
votes

Finally i am able to resolve this issue, this was happening because jmockit was creating hierarchy for passing the Optional values,

in my case Optional values were passed till @BeforeClass but was not able to pass @optional Values in @BeforeMethod because we were not using the Dataprovider in test.

We have removed the jmockit and now its working fine