遇到的問題:
jmeter 在使用beanshell PreProcessor 前置處理器時,編寫了以下腳本,執行后log 日志中報錯
import java.text.SimpleDateFormat; import java.util.Calendar; //獲取當前月份 log.info("獲取日期。。。。。。"); Calendar c = Calendar.getInstance(); String getCurrent=new SimpleDateFormat("yyyy-MM ").format(c.getTime()); vars.put("getCurrent",getCurrent); String getYear= c.get(Calendar.YEAR);//獲取當前年 vars.put("getYear",getYear); String getMonth=c.get(Calendar.MONTH) + 1; vars.put("getMonth",getMonth); String a=vars.get("getCurrent") ; log.info("getCurrent "+a); // 一年內的第xx周//獲取當前星期是第幾周 // System.out.println("當前時間的年周數:" + c.get(Calendar.WEEK_OF_YEAR));
報錯信息如下:
2021-10-13 08:55:05,846 INFO o.a.j.u.BeanShellTestElement: 獲取日期。。。。。。 2021-10-13 08:55:05,848 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.SimpleDateFormat; import java.util.Calendar; //獲取當前月份 log.info( . . . '' : Typed variable declaration 2021-10-13 08:55:05,848 WARN o.a.j.m.BeanShellPreProcessor: Problem in BeanShell script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.text.SimpleDateFormat; import java.util.Calendar; //獲取當前月份 log.info( . . . '' : Typed variable declaration
搜索到的解決辦法:
1.beanshell PreProcessor 代碼中有空行 ----未解決我的問題
2.log語句不能有空格,也不能把加號變為別的符號,改成如下的樣式就正常了。 log.info("url:"+url); --未解決我的問題
3.未加try…catch造成的。所執行的代碼有異常,但異常未進行捕獲處理。--成功解決了我的問題
修改后的代碼如下:
import java.text.SimpleDateFormat; import java.util.Calendar; //獲取當前月份 try{ log.info("獲取日期。。。。。。"); Calendar c = Calendar.getInstance(); String getCurrent=new SimpleDateFormat("yyyy-MM ").format(c.getTime()); vars.put("getCurrent",getCurrent); String getYear= c.get(Calendar.YEAR);//獲取當前年 vars.put("getYear",getYear); String getMonth=c.get(Calendar.MONTH) + 1; vars.put("getMonth",getMonth); String a=vars.get("getCurrent") ; log.info("getCurrent:"+a); System.out.println(a); // 一年內的第xx周//獲取當前星期是第幾周 // System.out.println("當前時間的年周數:" + c.get(Calendar.WEEK_OF_YEAR)); } catch(Exception e){ FailureMessage = "獲取日期信息異常"; Failure = true; }
記錄一下