有一个特定的要求,我需要将以前的日期时间版本与更新的日期时间版本从jmeter中的json响应进行比较
以下是我之前的回复:
{
"state":
{
"errorDetails":
[
]
},
"results":
[
{
"state":
{
"errorDetails":
[
]
},
"id":"someNumber1",
"version":"2017-11-23T15:25:20.154+00:00",
"referenceId":"1234"
}
]
}
更新后的回复:
{
"state":
{
"errorDetails":
[
]
},
"results":
[
{
"state":
{
"errorDetails":
[
]
},
"id":"someNumber1",
"version":"2017-11-23T15:25:22+00:00",
"referenceId":"1234"
}
]
}
我正在通过JSON Extractor提取版本并将其传递给BeanShell断言以下:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
try{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
long created = sdf.parse(vars.get("noteVersion")).getTime();
long updated = sdf.parse(vars.get("updatedNoteVersion")).getTime();
if ( updated > created){
SampleResult.setResponseMessage("PASSED");
}else{
SampleResult.setResponseMessage("FAIL");
}
}
catch (Exception ex){
log.warn("Error in my script", ex);
throw ex; // elsewise JMeter will "swallow" the above exception
}
但是我无法解析日期时间并且遇到以下错误:
java.text.ParseException: Unparseable date: "2017-11-23T15:25:20.154+00:00"
at java.text.DateFormat.parse(Unknown Source) ~[?:1.8.0_31]
at sun.reflect.GeneratedMethodAccessor77.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
at bsh.Reflect.invokeMethod(Reflect.java:134) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.Reflect.invokeObjectMethod(Reflect.java:80) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.Name.invokeMethod(Name.java:858) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHMethodInvocation.eval(BSHMethodInvocation.java:75) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHPrimarySuffix.doSuffix(BSHPrimarySuffix.java:103) ~[bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:80) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:47) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:86) ~[bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:84) ~[bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHBlock.evalBlock(BSHBlock.java:130) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHBlock.eval(BSHBlock.java:80) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHBlock.eval(BSHBlock.java:46) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.BSHTryStatement.eval(BSHTryStatement.java:86) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.Interpreter.eval(Interpreter.java:645) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.Interpreter.eval(Interpreter.java:739) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at bsh.Interpreter.eval(Interpreter.java:728) [bsh-2.0b5.jar:2.0b5 2005-06-12 04:50:41]
at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_31]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_31]
at org.apache.jmeter.util.BeanShellInterpreter.bshInvoke(BeanShellInterpreter.java:166) [ApacheJMeter_core.jar:3.3 r1808647]
at org.apache.jmeter.util.BeanShellInterpreter.eval(BeanShellInterpreter.java:189) [ApacheJMeter_core.jar:3.3 r1808647]
at org.apache.jmeter.util.BeanShellTestElement.processFileOrScript(BeanShellTestElement.java:151) [ApacheJMeter_core.jar:3.3 r1808647]
at org.apache.jmeter.assertions.BeanShellAssertion.getResult(BeanShellAssertion.java:105) [ApacheJMeter_components.jar:3.3 r1808647]
at org.apache.jmeter.threads.JMeterThread.processAssertion(JMeterThread.java:812) [ApacheJMeter_core.jar:3.3 r1808647]
at org.apache.jmeter.threads.JMeterThread.checkAssertions(JMeterThread.java:803) [ApacheJMeter_core.jar:3.3 r1808647]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:525) [ApacheJMeter_core.jar:3.3 r1808647]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:424) [ApacheJMeter_core.jar:3.3 r1808647]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:255) [ApacheJMeter_core.jar:3.3 r1808647]
除此之外,我能够达到正常的日期时间要求,因为yyyy-MM-DD HH:mm:ss - (2017-11-23 15:25:20)
任何人都可以帮我这个
答案 0 :(得分:0)
我建议您放弃已久的SimpleDateFormat
和朋友,并使用java.time
,即现代Java日期和时间API,也称为JSR-310。它通常更好用,并且在你的特定情况下有一些很好的优点,我们将会看到。
String noteVersion = "2017-11-23T15:25:20.154+00:00";
String updatedNoteVersion = "2017-11-23T15:25:22+00:00";
OffsetDateTime created = OffsetDateTime.parse(noteVersion);
OffsetDateTime updated = OffsetDateTime.parse(updatedNoteVersion);
if (updated.isAfter(created)) {
System.out.println("PASSED");
} else {
System.out.println("FAILED");
}
使用您问题的数据时间版本字符串,代码段打印
PASSED
第一个优势:来自JSON的日期时间格式是ISO 8601,一种用于将日期和时间信息作为文本交换的标准格式,以及JSON中经常使用的其他位置。包括java.time
在内的OffsetDateTime
类将此格式解析为默认格式,即没有任何明确的格式规范。无需担心偏移是Z
还是XXX
还是其他。
第二个优势:ISO 8601足够灵活,允许时间有或没有毫秒,因此解析适用于您之前和更新的日期时间(使用SimpleDateFormat
您需要两个SimpleDateFormat
实例)。
问题:我可以在Java版本中使用现代API吗?
如果至少使用Java 6 ,则可以。