2016-06-28 24 views
0

當我只是提取參數測試經過正確 當我嘗試操縱參數一切都出錯RunTimeException! 幫助理解錯誤!testNG @BeforeMethod使用湖參數ITestResult提取參數;

@BeforeMethod 
    public void beforeMethodBlock(ITestResult result){ 
     ReportGenerator.add(result.getParameters());  
    } 
public class ReportGenerator { 
    private static Object[][] testData= new Object[50][2]; 
    private static Integer index; 
    public static void add(Object[] data){ 
     testData[index++]=data; 

    } 
} 
+1

你能分享異常的全堆棧跟蹤? – juherr

+0

如果我只是在添加方法中打印參數程序失敗! – Artur

+0

java.lang.RuntimeException – Artur

回答

0

看起來你index變量從未被初始化,您使用Integer,而不是int(爲什麼?!)的默認值是null。請使用int並初始化它。

+0

問題不在int如果我只是打印(「」+ data [0])它出錯我有印象,它不可能在類中使用BeforeMethod註釋和在listener中編寫一些語句! libs – Artur

+0

根據源代碼,測試結果沒有狀態,不應該發生什麼。如果你使用的是testng 6.9.12,你可以在github上打開一個問題嗎? – juherr

+0

int而不是Integer作品!但如果我寫System.out.println(「」+ data [0]);出錯。 – Artur

0
  • BeforeMethod TestNG中可以有ITestContext的參數
  • 後,方法可以有ItestResult

的參數,如果你必須得到TestMethod的名稱是下一個要執行的,可以做通過方法(java.lang.reflect.Method)對象。

@BeforeMethod public void beforeMethod(Method m) { System.out.println(m.getName()); }

@BeforeMethod public void beforeMethod(ItestContext testContext) { // Do testContext related processing }

參考: -
How do I get the name of the test method that was run in a testng tear down method?