java - @Optional 注释未在 TestNG 中使用 @BeforeMethod 注释传递指定值

标签 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。

但如果我使用其他 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/

相关文章:

java - selenium 的自定义 JAVA 位置

python - 在 behave python 中定义上下文变量

java - 如何按源代码顺序获取方法

java - BuildConfig.DEBUG(或等效项)作为常量值

java - JVM如何解析java中的重写和覆盖方法

java - 用于生产的好的断言类? Java 相当于 Groovy 的 PowerAssert?

java - Maven 故障安全插件不运行并行测试

Java @interface.List 组在自定义约束注释中不起作用

java - makeText 方法,何时使用此方法以及何时使用 getBaseContext()

java - StringTemplate 违反分离?