我目前正在使用 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
输入 Before Test is=Krishna
输入 Before class is=Krishna
在方法之前输入 is=
测试方法
在方法之后输入 is=
输入 After class is=Krishna
输入 After Test is=Krishna
我通过右键单击类然后选择以 testNG 运行来执行此类。
请帮助我确定为什么在 aftertest 和 beforetest 中传递 null
最佳答案
最后我能够解决这个问题,这是因为 jmockit 正在创建用于传递可选值的层次结构,
在我的例子中,可选值被传递到 @BeforeClass
但无法在 @BeforeMethod
中传递 @optional Values
因为我们没有在测试中使用 Dataprovider
。
我们已经删除了 jmockit,现在它可以正常工作了
关于java - @Optional 注释未在 TestNG 中使用 @BeforeMethod 注释传递指定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26494220/