title: Jenkins DSL Jobs: GroovyCastException Cannot cast object
body: <p>I am new to groovy and to Jenkins DSL jobs. I am trying to use a groovy script to create jobs that run test scripts. I am running into a Cannot cast object error and am not sure what the issue is and where the casting is getting thrown off. The error message doesnt seem to point to a specific line, bit I thought it might be an issue with the class not importing, but it seems like it is. It seems like the error has to do with the JenkinsDSL file.</p>
  
  <p>Below is are the two files that I created and the error message. The JenkinsDSL.groovy contains a method that takes in parameters to create the job.</p>
  
  <p>Thank you in advance!</p>
  
  <p><strong>Update</strong>
  I ran it through this website: <a href="http://job-dsl.herokuapp.com/" rel="nofollow noreferrer">http://job-dsl.herokuapp.com/</a> and it confirms that the issue is in JenkinsDSL.groovy</p>
  
  <p><strong>JenkinsDSL.groovy</strong></p>
  
  <pre><code>class JenkinsDSL {
      String gitUrl, gitRemoteName, gitBranch, gitCredentials
      String project, testSuite, testCaseName, testCaseDesc
  
      def addJob(def dslFactory, String project, String testSuite, String testCaseName, String testCaseDesc) {
  
          dslFactory.folder("Selenium Tests") {
              displayName("Selenium Tests")
              description("someDescription")
          }
  
          dslFactory.listView(project) {
             description("Tests for ${project}")
              filterBuildQueue()
              filterExecutors()
              jobs { 
              }
              columns {
                  status()
                  weather()
                  name()
                  lastSuccess()
                  lastFailure()
                  lastDuration()
                  buildButton()
              }
          }
  
          dslFactory.folder(testSuite) {
              displayName(testSuite)
              description("Verifies functionality related to ${testSuite}")
          }
  
          dslFactory.mavenJob("Selenium Tests/${project}/${testSuite}/${testCaseName}") {
              description("${testCaseDesc}")
              label('AWS-Linux-Java-7')
              wrappers {
                  timeout {
                      absolute(5)
                  }
              }
              scm {
                  git {
                      remote {
                          name("${gitRemoteName}")
                          url("${gitUrl}")
                          credentials("${gitCredentials}")
                      }
                      branch("${gitBranch}")
                  }
              }
              parameters {
                  choiceParam('Browser', ['Chrome', 'Firefox', 'Safari'])
                  choiceParam('Environment', ['Dev', 'Test'])
                  choiceParam('TestCase', ["${testCaseName}"])
              }
              triggers {
                  parameterizedCron('''
                      H 0 * * * % Browser=Chrome;Environment=Dev;TestCase=TestCase
                      H 0 * * * % Browser=Firefox;Environment=Dev;TestCase=TestCase
                      H 0 * * * % Browser=Safari;Environment=Dev;TestCase=TestCase
                      H 0 * * * % Browser=Chrome;Environment=Test;TestCase=TestCase
                      H 0 * * * % Browser=Firefox;Environment=Test;TestCase=TestCase
                      H 0 * * * % Browser=Safari;Environment=Test;TestCase=TestCase
                  ''')
              }
  
              rootPOM("pom.xml")
              goals("clean test -DBrowser='$BROWSER' -DEnvironment='$ENVIRONMENT' -DTestCase='$TESTCASE'");
  
              publishers {
                  archiveArtifacts {
                      pattern('target/surefire-reports/customized-emailable-report.html')
                      pattern('target/surefire-reports/emailable-report.html')
                      pattern('target/surefire-reports/index.html')
                      pattern('target/surefire-reports/screenshots/**')
                  }
                  wsCleanup {
                  includePattern('target/surefire-reports/**')
                  deleteDirectories(true)
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>PortalDSL.groovy</strong></p>
  
  <pre><code>import JenkinsDSL;
  
  def job = new JenkinsDSL(
      gitUrl: 'giturl',
      gitRemoteName: 'gitname',
      gitBranch: 'gitbranch',
      gitCredentials: 'gitcreds'
  )
  
  /********************************** Project 1 **********************************/
  
  /***************** Test Suite 1 *****************/
  
  /*** Test Case 1 ***/
  job.addJob(
      this,
      'projectName',
      'testSuite',
      'testCase1',
      'Some Description'
  )
  /*** Test Case 2 ***/
  job.addJob(
       this,
      'projectName',
      'testSuite',
      'testCase2',
      'Some Description'
  )
  </code></pre>
  
  <p><strong>Jenkins Error Message</strong></p>
  
  <pre><code>Processing DSL script JenkinsDSL.groovy
  org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'org.codehaus.groovy.runtime.InvokerHelper$1@620c0d02' with class 'org.codehaus.groovy.runtime.InvokerHelper$1' to class 'javaposse.jobdsl.dsl.JobParent'
  at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
  at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
  at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
  at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.castToType(ScriptBytecodeAdapter.java:603)
  at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScriptEngine(AbstractDslScriptLoader.groovy:103)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
  at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:384)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
  at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:69)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:174)
  at javaposse.jobdsl.dsl.AbstractDslScriptLoader$_runScripts_closure1.doCall(AbstractDslScriptLoader.groovy:59)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
  at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1022)
  at groovy.lang.Closure.call(Closure.java:414)
  at groovy.lang.Closure.call(Closure.java:430)
  at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2040)
  at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2025)
  at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:2078)
  at org.codehaus.groovy.runtime.dgm$164.invoke(Unknown Source)
  at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:274)
  at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:56)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
  at javaposse.jobdsl.dsl.AbstractDslScriptLoader.runScripts(AbstractDslScriptLoader.groovy:46)
  at javaposse.jobdsl.plugin.ExecuteDslScripts.perform(ExecuteDslScripts.java:323)
  at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
  at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
  at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
  at hudson.model.Build$BuildExecution.build(Build.java:206)
  at hudson.model.Build$BuildExecution.doRun(Build.java:163)
  at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
  at hudson.model.Run.execute(Run.java:1727)
  at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
  at hudson.model.ResourceController.execute(ResourceController.java:97)
  at hudson.model.Executor.run(Executor.java:429)
  ERROR: Cannot cast object 'org.codehaus.groovy.runtime.InvokerHelper$1@620c0d02' with class 'org.codehaus.groovy.runtime.InvokerHelper$1' to class 'javaposse.jobdsl.dsl.JobParent'
  Finished: FAILURE
  </code></pre>