Reputation: 13489
The goal: to assign a SoapUI property that has a value of today + 1 year in the format yyyy-MM-dd
(e.g., today is 2018-10-10 so I would like the property to have 2019-10-10).
Attempt #1 (in-line property):
Doesn't work because JDK 8 doesn't seem to be part of the default Soap UI package:
${=LocalDate.now().plusYears(1).format(DateTimeFormatter.ISO_DATE)}
Attempt #2 (groovy script):
Returns an error because Date.format
doesn't take a String
and java.util.Date
:
def today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
today.add(Calendar.YEAR, 1);
def nextYear = today.getTime();
def nextYear_formatted = Date.format("yyyy-MM-dd", nextYear);
testRunner.testCase.setPropertyValue( "nextYear", nextYear_formatted )
error:
groovy.lang.MissingMethodException: No signature of method: static java.util.Date.format() is applicable for argument types: (java.lang.String, java.util.Date) values: [yyyy-MM-dd, Thu Oct 10 00:00:00 EDT 2019] Possible solutions: format(java.lang.String, java.util.TimeZone), format(java.lang.String), from(java.time.Instant) error at line: 8
Attempt #3 (groovy script):
Returns an error because SimpleDateFormat
doesn't seem to compile:
def today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
today.add(Calendar.YEAR, 1);
def nextYear = today.getTime();
def nextYear_formatted = new SimpleDateFormat("yyyy-MM-dd").format(nextYear);
testRunner.testCase.setPropertyValue( "nextYear", nextYear_formatted )
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script20.groovy: 8: unable to resolve class SimpleDateFormat @ line 8, column 27. def nextYear_formatted = new SimpleDateFormat("yyyy-MM-dd").format(nextYear); ^ org.codehaus.groovy.syntax.SyntaxException: unable to resolve class SimpleDateFormat @ line 8, column 27. at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:149) at ...
(rest of stack trace omitted for brevity)
How do you add a property that has a value of today + 1 year in SoapUI?
Bonus question: how do you add JDK 8 so you can use it in groovy scripts?
Upvotes: 1
Views: 1782
Reputation: 3932
Do you have groovy available?
def nextYear = use( groovy.time.TimeCategory ) { new Date() + 1.year }.format( 'yyyy-MM-dd' )
Upvotes: 2
Reputation: 13489
Further research yielded the answer: SimpleDateFormat
needs java.text.
in front of it. So
def today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
today.add(Calendar.YEAR, 1);
def nextYear = today.getTime();
def nextYear_formatted = new java.text.SimpleDateFormat("yyyy-MM-dd").format(nextYear);
testRunner.testCase.setPropertyValue( "nextYear", nextYear_formatted )
works.
Upvotes: 1