【问题标题】:@Optional annotation is not passing specified values with @BeforeMethod annotation in TestNG@Optional 注释未在 TestNG 中使用 @BeforeMethod 注释传递指定的值
【发布时间】: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


【解决方案1】:

我终于能够解决这个问题,这是因为 jmockit 正在创建用于传递可选值的层次结构,

在我的例子中,可选值被传递到@BeforeClass,但无法在@BeforeMethod 中传递@optional Values,因为我们没有在测试中使用Dataprovider

我们已经删除了 jmockit,现在它可以正常工作了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多