【发布时间】:2014-10-21 18:57:16
【问题描述】:
我目前正在研究 TestNG 框架并面临的问题 -
我已经声明了一个在类中的每个测试之前运行的方法,方法是声明一个类似
的方法@BeforeMethod
@Parameters({"type"})
public void beforeMethod(@Optional("Krishna") String type)
{
System.out.println("Type in Before Method is="+type);
}
这里我得到这个方法里面的值类型是NULL。
但是,如果我使用其他 testNG 注释,如 @BeforeSuite、@BeforeTest、@BeforeClass 工作正常,但 @Optional 注释没有传递 @BeforeMethod 和 @AfterMethod 注释的默认值。
我的完整测试课程是:
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);
}
}
输出是
输入Before suite is=Krishna
在测试前输入=Krishna
在课前输入=Krishna
输入之前的方法是=
测试方法
输入 After Method is=
键入 After class is=Krishna
输入 After Test is=Krishna
我已经通过右键单击类执行了这个类,然后选择作为 testNG 运行。
请帮助我确定为什么在后测和前测中传递了 null
【问题讨论】:
-
我不认为
null被传递到那里,很可能是一个空字符串 ("")。您使用哪个版本的 TestNG?我的猜测是:由于您的test()方法没有参数,因此 BeforeTest 和 AfterTest 使用了某种默认值作为参数,因此未应用Optional。
标签: java testing annotations automated-tests testng