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
null
was passed there, most probably an empty String (""
). Which version of TestNG do you use? My guess would be: as yourtest()
method has no parameters, so some kind of default was used for BeforeTest and AfterTest as a parameter, soOptional
was not applied. – Gábor Bakos