In groovy I was using below to generate date in the format "2020-09-14T06:00:00Z" in Ready API 3.3.1
def today = new Date()
return today.format("yyyy-MM-dd") + "T" + today.format("HH:mm:ss") +".000Z"
After updating to 3.3.2 It's giving me an error
groovy.lang.MissingMethodException: No signature of method: java.util.Date.format()
is applicable for argument types: (String) values: [yyyy-MM-dd].
Could someome help
In recent versions of Groovy the Date extension methods have been moved to their own library, which you will have to add to your project. For example, if you are using Gradle as your build tool, you may want something like this:
compile group: 'org.codehaus.groovy', name: 'groovy-dateutil', version: '3.0.5'
If using Maven:
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-dateutil</artifactId>
<version>3.0.5</version>
</dependency>
Related
I try to add HttpBuilder into groovy script, but can do it only manually (Alt+Ctrl+Shift+S add dependencie). But when I start script I have error in line of creating new httpbuilder instance java.lang.ClassNotFoundException: org.apache.http.client.HttpClient. I manualy add HttpClient, butClassNotFoundException: net.sf.json.JSONObject and so on. But when I add Ini library it works fine.
I also tried to use #Grab
main()
def main() {
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org')
And have compilation error
Error:Groovyc: While compiling GroovyTests: java.lang.RuntimeException: Error grabbing Grapes -- [download failed: net.sf.json-lib#json-lib;2.3!json-lib.jar]
And net in def http = new groovyx.net.http.HTTPBuilder('http://www.codehaus.org') is red and Cannot resolve a symbol 'net' error
will be glad to any help
Since you have now installed the groovy executables as per the comments, the following code:
#Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7')
import groovyx.net.http.HTTPBuilder
def http = new HTTPBuilder('https://jsonplaceholder.typicode.com')
def res = http.get(path: '/users')
println "Number of users: ${res.size()}"
should now run and print:
─➤ groovy solution.groovy
Number of users: 10
─➤
(tested on Groovy Version: 2.5.8 JVM: 1.8.0_232 Vendor: AdoptOpenJDK OS: Linux)
One thing that might be disrupting the artifact resolution is if you have a custom grapeConfig.xml file. This file (if it exists) should be under <user home dir>/.groovy/grapeConfig.xml and the default text that groovy uses if no grapeConfig.xml file is present can be found here:
https://github.com/apache/groovy/blob/master/src/resources/groovy/grape/defaultGrapeConfig.xml
In addition, if you need to debug grapes downloads, you should try the following flags (again as mentioned in the comments):
─➤ groovy -Dgroovy.grape.report.downloads=true -Divy.message.logger.level=4 yourGroovyScript.groovy
which should print information on what grapes are actually doing when the resolution fails.
What does your groovy -v look like? i.e. what version of groovy and what jdk are you on?
I am trying to execute a 2nd round of running only the failed scenarios in order to avoid false alarms because of environment/network/test unstabilities.
I am wondering if there is way to run only a maven command that will perform the following:
1. run all the scenarios that my runner includes in its configuration
2. create the cucumber.json report for this run
3. create a list of the failed scenarios
4. run the list of the failed scenarios
5. change the result of the report created in 1rst run depending on the results of failed tests in 2nd run, so that at the end, only the scenarios that failed in both runs are marked as failed and also there are no duplicates in the final report for those tests that failed at 1rst run (i.e. 2nd run overides the result of 1st run).
I am using cucumber 4.4.0 and testNG runner.
I have tried to use #ExtendedCucumberOptions (http://mkolisnyk.github.io/cucumber-reports/extended-cucumber-runner) and the 1rst run was ok, but the 2nd run was never happened, throwing the following exception:
"Method public void com.github.mkolisnyk.cucumber.runner.ExtendedTestNGRunner.feature(cucumber.api.testng.CucumberFeatureWrapper) requires a #DataProvider named : feature"
I found this issue:
https://github.com/mkolisnyk/cucumber-reports/issues/138
So, it seems that this cucumber-reports library does not support later versions of cucumber and I cannot use #ExtendedCucumberOptions that seemed to provide exactly what I need.
===> My maven dependecies include:
<dependency>
<groupId>com.github.mkolisnyk</groupId>
<artifactId>cucumber-runner</artifactId>
<version>1.3.4</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>4.4.0</version>
</dependency>
===> And my runner class :
package runners;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import org.testng.annotations.DataProvider;
#CucumberOptions(
features = {"src/test/resources/features/"},
glue = {"stepDefinitions"},
tags = {"#magic"},
plugin = {
"pretty"
, "html:target/cucumber"
, "json:target/cucumber/cucumber.json"
, "rerun:target/cucumber/failedTests.txt"
},
monochrome = false
)
public class AreaRunner extends AbstractTestNGCucumberTests {
#Override
#DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
Is there any solution other than #ExtendedCucumberOptions?
The reason the 2nd run never happened is because Retry functionality is applicable for JUnit runner only, as per https://github.com/mkolisnyk/cucumber-reports/issues/167.
Try https://github.com/prashant-ramcharan/courgette-jvm - it has the ability to rerun failing tests similarly to ExtendedCucumberOptions, but this one contains the new Cucumber already.
I wrote a very simple groovy script to test if a cron expression is valid:
import hudson.scheduler.CronTabList
try {
def cron = CronTabList.create("#daily")
println("Valid cron!")
} catch(Exception e) {
println("Invalid cron!")
e.printStackTrace()
}
Running this fails with the message:
Caught: java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
java.lang.NoClassDefFoundError: javax/servlet/ServletContextListener
at hudson.scheduler.BaseParser.<clinit>(BaseParser.java:149)
at hudson.scheduler.CronTab.set(CronTab.java:113)
at hudson.scheduler.CronTab.<init>(CronTab.java:100)
at hudson.scheduler.CronTabList.create(CronTabList.java:121)
at hudson.scheduler.CronTabList.create(CronTabList.java:96)
at hudson.scheduler.CronTabList$create.call(Unknown Source)
at validate_crontab.run(validate_crontab.groovy:7)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContextListener
... 7 more
Process finished with exit code 1
My build.gradle dependencies look like:
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
compile group: 'org.quartz-scheduler', name: 'quartz', version: '2.3.0'
compile group: 'org.jenkins-ci.main', name: 'jenkins-core', version: '2.85'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
I simply can't figure out what's to blame and why I can't run the script.
Any help is much appreciated!
Apparently what you miss is the servlet API. For example:
dependencies {
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}
If you put that in your dependencies your script will most likely run.
But I guess that it is fairly important to understand why did you get this error. If you have a closer look at the jenkins-core library you will notice that it has a "provided" dependency to the servlet API. A provided dependency in simpler words means that the library (jenkins-core in your case) is compiled with the assumption that the servlet API jar will be present in the class path at your production environment - e.g. when using the lib in a web application running within a servlet container.
I guess that you run your groovy script as a standalone app, that is why you are getting an error. And... DISCLAIMER - I do not know if using jenkins-core in standalone apps is intended, though :-).
I'm attempting to create a small Springboot application that uses the groovy.sql.Sql class to connect to an Oracle database(the Oracle jar has already been grabbed and is in the spring boot classpath). This is a very simple example for proof of concept/testing.
import groovy.sql.Sql
#RestController
class ThisWillActuallyRun {
#RequestMapping("/")
String home() {
oracleSql = Sql.newInstance(jdbc:oracle:thin:#oracle-db:1521:db-name,
"oracle-user",
"oracle-pass",
"oracle.jdbc.driver.OracleDriver")
row = oracleSql.firstRow("select foo from blah")
return "ok"
}
}
When the application is ran using the command:
spring run test_for_so.groovy
The following error is produced:
startup failed:
file:test_for_so.groovy: 1: unable to resolve class groovy.sql.Sql # line 1, column 1.
import groovy.sql.Sql
What groovy jars are you including as dependencies? The groovy jar only includes the base language support. Change it to groovy-all to get the full language and library package. For example, in gradle, use:
dependencies {
compile "org.codehaus.groovy:groovy-all"
...
}
In maven, it would look like this:
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
</dependencies>
You can also just add the groovy-sql on top of the groovy package if you don't want everything.
I had same problem. I just found the solution.
Add below code.
#Grab('groovy-sql')
After groovy-sql you can use
import groovy.sql.Sql
I tried Groovy TimeCategory Mixin according to Groovy Coodbook
import groovy.time.TimeCategory
Integer.metaClass.mixin TimeCategory
Date.metaClass.mixin TimeCategory
footballPractice = 1.week.from.now - 4.days + 2.hours - 3.seconds
println footballPractice
It works fine in the groovy console, but I get a StackOverError, when I run it as a script
groovy MyScript.groovy
Using
Groovy Version: 2.1.8 (via GVM tool, but the same problem with system default 1.8.x)
JVM: 1.7.0_40 Vendor: Oracle Corporation
OS: Linux, Ubuntu 13.04, 64bit
I will double check but I don't think mixin is recommended anymore. Try
use(TimeCategory) {
footballPractice = 1.week.from.now - 4.days + 2.hours - 3.seconds
println footballPractice
}