Java @可选批注未通过TestNG中的@BeforeMethod批注传递指定值
我目前正在从事TestNG框架的工作,并面临以下问题:- 我已经声明了一个在类中的每个测试之前运行的方法,方法如下Java @可选批注未通过TestNG中的@BeforeMethod批注传递指定值,java,testing,annotations,automated-tests,testng,Java,Testing,Annotations,Automated Tests,Testng,我目前正在从事TestNG框架的工作,并面临以下问题:- 我已经声明了一个在类中的每个测试之前运行的方法,方法如下 @BeforeMethod @Parameters({"type"}) public void beforeMethod(@Optional("Krishna") String type) { System.out.println("Type in Before Method is="+type); } 在这里,我得到这个方法中的值类型为NULL 但是,如果我使用其他te
@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);
}
}
输出是
在组曲is=Krishna之前输入
测试前输入is=Krishna上课前输入is=Krishna
在方法为之前键入=
试验方法
在方法is=
之后键入 课后输入is=Krishna
测试后输入is=Krishna
我通过右键单击类,然后选择RunasTestNG来执行这个类
请帮助我确定在后测和前测中传递null的原因最后,我能够解决这个问题,之所以发生这种情况,是因为jmockit正在创建传递可选值的层次结构 在我的例子中,可选值一直传递到
@BeforeClass
,但无法传递@beforethod
中的@可选值,因为我们在测试中没有使用数据提供程序
我们已经删除了jmockit,现在它工作正常了我不认为传递了null
,很可能是一个空字符串(“”
)。您使用哪个版本的TestNG?我的猜测是:由于您的test()
方法没有参数,因此在BeforeTest和BeforeTest中使用了某种默认值作为参数,因此没有应用Optional
。