GroovyCastException:无法将类为[另一个晦涩的闭包]的对象[某些晦涩的闭包]强制转换为[您的业务接口/类]

时间:2018-06-29 13:52:03

标签: groovy katalon-studio

我有一个松耦合的代码结构,可用来为Katalon Studio测试提供动力。它由一个TaskInfoListPayRateInterface组成。另外,TaskInfo将来会实现TaskInterface(因为将有一个具有TaskInterface的类)。

在此方法的第一行中,TaskInfo

@Override
public PayRateInterface getMostRecentPayRate() {
    PayRateInterface newestPayrate = payRates.get(0);
    for (PayRateInterface payRate : payRates) {
        if (payRate.getTimestamp().after(newestPayrate.getTimestamp())) {
            newestPayrate = payRate;
        }
    }
    return newestPayrate;
}

,我得到运行时异常:

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'

PayRateInterface的定义如下:

public interface PayRateInterface {
    double getPayRate();
    Date getTimestamp();
}

其余的类和接口定义如下:

PayRateInfo

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 >>> 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;
    }
}

TaskInterface

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();
}

TaskInfo

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<PermissionGroupInterface> permissionGroups = new LinkedList<PermissionGroupInterface>();
    private List<PayRateInterface> payRates = new LinkedList<PayRateInterface>();
    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<PayRateInterface> payRates) {
        this.payRates = payRates;
    }

    /**
     * 
     * @param name
     * @param payRates
     * @param permissionGroups
     */
    public TaskInfo(String name, List<PayRateInterface> payRates, List<PermissionGroupInterface> 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<PermissionGroupInterface> getPermissionGroups() {
        return permissionGroups;
    }




    public void setPermissionGroups(List<PermissionGroupInterface> permissionGroups) {
        this.permissionGroups = permissionGroups;
    }




    public List<PayRateInterface> getPayRates() {
        return payRates;
    }




    public void setPayRates(List<PayRateInterface> 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;
    };
}

用例

我想说

TaskInfo noName = new TaskInfo('', Arrays.asList({ 
        new PayRateInfo(15.00)
    }))

然后就像

noName.getMostRecentPayRate()

如果有的话,我在做错什么吗?

0 个答案:

没有答案