簡體   English   中英

在JMeter中的BeanShell Sampler中將字符串解析為整數

[英]Parsing string to integer in BeanShell Sampler in JMeter

我正在嘗試在JMeter中將字符串解析為整數但由於跟隨錯誤而失敗。 如果我嘗試打印vars.get返回的字符串,它們看起來很好。

2014/06/28 00:08:52 WARN  - jmeter.assertions.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval  Sourced file: inline evaluation of: ``if (ResponseCode != null && ResponseCode.equals ("200") == false ) {  int i = In . . . '' : Typed variable declaration : Method Invocation Integer.parseInt 

以下是我的代碼

if (ResponseCode != null && ResponseCode.equals ("200") == false )
{
    int i = Integer.parseInt(vars.get("currentPMCount"));
    int j = Integer.parseInt(vars.get("pmViolationMaxCount"));
    if( i > j ){
        log.warn("PM count on server is greater than max allowed count.");
        }

        log.warn( "The return code is " + ResponseCode); // this goes to the JMeter log file
} 
else 
{
    Failure=true ;
    FailureMessage = "The response data size was not as expected" ;   
}

您的代碼看起來不錯,但它可能是currentPMCount和/或pmViolationMaxCount變量的問題。

如果它們看起來很好並且看起來像整數並且不超過Integer的最大/最小值,您可以嘗試以下方法:

  1. 確保數字值周圍沒有“空格”字符,因為前導或尾隨空格會導致轉換失敗。 也許在變量上調用trim()方法可以幫助:

     int i = Integer.parseInt(vars.get("currentPMCount").trim()); 
  2. 如果將腳本存儲到文件中,然后在Beanshell斷言中提供文件路徑,則會出現“有問題”的行號
  3. 我最喜歡的:將代碼包圍到try / catch塊中,如下所示:

     try{ //your code here } catch (Exception ex){ log.warn("Error in my script", ex); throw ex; // elsewise JMeter will "swallow" the above exception } 

通過這種方式,您將獲得更多信息性的堆棧跟蹤而不是糟糕的Error invoking bsh method消息,它什么都不說。

請參閱如何使用BeanShell:JMeter最喜歡的內置組件指南,以獲取更多提示和技巧。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM