title: GroovyCastException: Cannot cast object [some obscure closure] with class [another obscure closure] to [your business interface/class]
body: <p>I have a loosely-coupled code structure that I'm using to power my Katalon Studio tests. It consists of a <code>TaskInfo</code> that has a <code>List</code> of <code>PayRateInterface</code>s. Also, <code>TaskInfo</code> implements <code>TaskInterface</code> (as there will be a class that has <code>TaskInterface</code>) in the future. </p>
  
  <p>At the very first line of this method in <code>TaskInfo</code>: </p>
  
  <pre><code>@Override
  public PayRateInterface getMostRecentPayRate() {
      PayRateInterface newestPayrate = payRates.get(0);
      for (PayRateInterface payRate : payRates) {
          if (payRate.getTimestamp().after(newestPayrate.getTimestamp())) {
              newestPayrate = payRate;
          }
      }
      return newestPayrate;
  }
  </code></pre>
  
  <p>, I get the runtime exception: </p>
  
  <pre><code>Test Cases/AddInvalidTasks FAILED because (of) org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Script1530218543089$_run_closure1@1358b28e' with class 'Script1530218543089$_run_closure1' to class 'com.ezsoftpos.beans.employee.tasks_and_rates.PayRateInterface'
  </code></pre>
  
  <p><code>PayRateInterface</code> is defined as follows: </p>
  
  <pre><code>public interface PayRateInterface {
      double getPayRate();
      Date getTimestamp();
  }
  </code></pre>
  
  <p>The rest of my classes and interfaces are defined as follows: </p>
  
  <p><code>PayRateInfo</code> : </p>
  
  <pre><code>package com.ezsoftpos.beans.employee.tasks_and_rates
  
  import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
  import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
  import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
  import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
  
  import com.kms.katalon.core.annotation.Keyword
  import com.kms.katalon.core.checkpoint.Checkpoint
  import com.kms.katalon.core.checkpoint.CheckpointFactory
  import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
  import com.kms.katalon.core.model.FailureHandling
  import com.kms.katalon.core.testcase.TestCase
  import com.kms.katalon.core.testcase.TestCaseFactory
  import com.kms.katalon.core.testdata.TestData
  import com.kms.katalon.core.testdata.TestDataFactory
  import com.kms.katalon.core.testobject.ObjectRepository
  import com.kms.katalon.core.testobject.TestObject
  import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
  import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
  
  import internal.GlobalVariable
  
  import MobileBuiltInKeywords as Mobile
  import WSBuiltInKeywords as WS
  import WebUiBuiltInKeywords as WebUI
  
  /**
   * Represents a pay rate. 
   * @author mwarren
   *
   */
  public class PayRateInfo implements PayRateInterface  {
      private double payRate;
      private Date timestamp = new Date();
  
      /**
       * No-args constructor
       */
      public PayRateInfo() {
      }
  
      /**
       * For just-added pay rates. 
       */
      public PayRateInfo(double payRate) {
          this.payRate = payRate;
      }
  
      /**
       * For retrieving a pay rate.
       * @param payRate
       * @param timestamp
       */
      public PayRateInfo(double payRate, Date timestamp) {
          this.payRate = payRate;
          this.timestamp = timestamp;
      }
  
      public double getPayRate() {
          return payRate;
      }
      public void setPayRate(double payRate) {
          this.payRate = payRate;
      }
      public Date getTimestamp() {
          return timestamp;
      }
      public void setTimestamp(Date timestamp) {
          this.timestamp = timestamp;
      }
  
      @Override
      public String toString() {
          return "PayRateInfo [payRate=" + payRate + ", timestamp=" + timestamp + "]";
      }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          long temp;
          temp = Double.doubleToLongBits(payRate);
          result = prime * result + (int) (temp ^ (temp &gt;&gt;&gt; 32));
          result = prime * result + ((timestamp == null) ? 0 : timestamp.hashCode());
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          PayRateInfo other = (PayRateInfo) obj;
          if (Double.doubleToLongBits(payRate) != Double.doubleToLongBits(other.payRate))
              return false;
          if (timestamp == null) {
              if (other.timestamp != null)
                  return false;
          } else if (!timestamp.equals(other.timestamp))
              return false;
          return true;
      }
  }
  </code></pre>
  
  <p><code>TaskInterface</code> : </p>
  
  <pre><code>package com.ezsoftpos.beans.employee.tasks_and_rates
  
  import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
  import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
  import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
  import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
  import com.kms.katalon.core.annotation.Keyword
  import com.kms.katalon.core.checkpoint.Checkpoint
  import com.kms.katalon.core.checkpoint.CheckpointFactory
  import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
  import com.kms.katalon.core.model.FailureHandling
  import com.kms.katalon.core.testcase.TestCase
  import com.kms.katalon.core.testcase.TestCaseFactory
  import com.kms.katalon.core.testdata.TestData
  import com.kms.katalon.core.testdata.TestDataFactory
  import com.kms.katalon.core.testobject.ObjectRepository
  import com.kms.katalon.core.testobject.TestObject
  import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
  import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
  
  import internal.GlobalVariable
  
  import MobileBuiltInKeywords as Mobile
  import WSBuiltInKeywords as WS
  import WebUiBuiltInKeywords as WebUI
  
  public interface TaskInterface {
      PayRateInterface getMostRecentPayRate();
  }
  </code></pre>
  
  <p><code>TaskInfo</code> : </p>
  
  <pre><code>package com.ezsoftpos.beans.employee.tasks_and_rates
  
  import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
  import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
  import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
  import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
  
  import com.ezsoftpos.beans.employee.permission_groups.PermissionGroupInterface
  import com.kms.katalon.core.annotation.Keyword
  import com.kms.katalon.core.checkpoint.Checkpoint
  import com.kms.katalon.core.checkpoint.CheckpointFactory
  import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords
  import com.kms.katalon.core.model.FailureHandling
  import com.kms.katalon.core.testcase.TestCase
  import com.kms.katalon.core.testcase.TestCaseFactory
  import com.kms.katalon.core.testdata.TestData
  import com.kms.katalon.core.testdata.TestDataFactory
  import com.kms.katalon.core.testobject.ObjectRepository
  import com.kms.katalon.core.testobject.TestObject
  import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
  import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords
  
  import internal.GlobalVariable
  
  import MobileBuiltInKeywords as Mobile
  import WSBuiltInKeywords as WS
  import WebUiBuiltInKeywords as WebUI
  
  /**
   * POJO for the state of a Task. 
   * @author mwarren
   *
   */
  public class TaskInfo implements TaskInterface {
      private List&lt;PermissionGroupInterface&gt; permissionGroups = new LinkedList&lt;PermissionGroupInterface&gt;();
      private List&lt;PayRateInterface&gt; payRates = new LinkedList&lt;PayRateInterface&gt;();
      private boolean isActive = true;
      private String name;
  
      /**
       * No-args constructor. 
       */
      public TaskInfo() {
      }
  
      /**
       * 
       * @param name
       */
      public TaskInfo(String name) {
          this.name = name;
      }
  
      /**
       * 
       * @param name
       * @param payRates
       */
      public TaskInfo(String name, List&lt;PayRateInterface&gt; payRates) {
          this.payRates = payRates;
      }
  
      /**
       * 
       * @param name
       * @param payRates
       * @param permissionGroups
       */
      public TaskInfo(String name, List&lt;PayRateInterface&gt; payRates, List&lt;PermissionGroupInterface&gt; permissionGroups) {
          this(payRates);
          this.permissionGroups = permissionGroups;
      }
  
  
      @Override
      public PayRateInterface getMostRecentPayRate() {
          PayRateInterface newestPayrate = payRates.get(0);
          for (PayRateInterface payRate : payRates) {
              if (payRate.getTimestamp().after(newestPayrate.getTimestamp())) {
                  newestPayrate = payRate;
              }
          }
          return newestPayrate;
      }
  
  
  
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public List&lt;PermissionGroupInterface&gt; getPermissionGroups() {
          return permissionGroups;
      }
  
  
  
  
      public void setPermissionGroups(List&lt;PermissionGroupInterface&gt; permissionGroups) {
          this.permissionGroups = permissionGroups;
      }
  
  
  
  
      public List&lt;PayRateInterface&gt; getPayRates() {
          return payRates;
      }
  
  
  
  
      public void setPayRates(List&lt;PayRateInterface&gt; payRates) {
          this.payRates = payRates;
      }
  
      public boolean isActive() {
          return isActive;
      }
  
      public void setActive(boolean isActive) {
          this.isActive = isActive;
      }
  
      @Override
      public String toString() {
          return "TaskInfo [permissionGroups=" + permissionGroups + ", payRates=" + payRates + "]";
      }
  
      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((payRates == null) ? 0 : payRates.hashCode());
          result = prime * result + ((permissionGroups == null) ? 0 : permissionGroups.hashCode());
          return result;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          TaskInfo other = (TaskInfo) obj;
          if (payRates == null) {
              if (other.payRates != null)
                  return false;
          } else if (!payRates.equals(other.payRates))
              return false;
          if (permissionGroups == null) {
              if (other.permissionGroups != null)
                  return false;
          } else if (!permissionGroups.equals(other.permissionGroups))
              return false;
          return true;
      };
  }
  </code></pre>
  
  <p><strong>Use Case</strong></p>
  
  <p>I tried to say</p>
  
  <pre><code>TaskInfo noName = new TaskInfo('', Arrays.asList({ 
          new PayRateInfo(15.00)
      }))
  </code></pre>
  
  <p>and then was like</p>
  
  <pre><code>noName.getMostRecentPayRate()
  </code></pre>
  
  <p>What, if anything, am I doing wrong?</p>