file-io - Groovy:无法将类 'true' 的对象 'java.lang.Boolean' 转换为类 'java.io.File'

标签 file-io groovy junit

我不明白为什么会出现以下错误。有什么想法吗?

我收到错误:
Cannot cast object 'true' with class 'java.lang.Boolean' to class 'java.io.File'
这是在“if (envProp.exists()...”行产生错误的代码:

static private File envProp = new File('env.properties')
static private File envPropBak = new File('env.properties.bak')

@BeforeClass
static void beforeAll() {
    if (envProp.exists()) {
        envPropBak.write( envProp.text )
    }
}

我不明白为什么envProp.exists()正试图将任何东西转换为另一个对象。方法.exists()应该只返回 boolean .

谢谢

最佳答案

我今天遇到了同样的问题,但就我而言是:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'true' with class 'java.lang.Boolean' to class 'java.util.List'

问题是,如果你有这样的事情:
public List<Foo> method(){
    methodThatReturnsTrue()
}

因为 Groovy 使用最后一句的返回值作为方法的返回值,所以它尝试将 true 转换为 <some_not_boolean_type>所以你和我得到的错误。

关于file-io - Groovy:无法将类 'true' 的对象 'java.lang.Boolean' 转换为类 'java.io.File',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17068689/

相关文章:

java - 在 Spring Integration 应用程序中,是否可以在链外测试 Spring Retry 机制?

groovy - Grails 检查映射中值的列表

java - 如何在项目编译之前将projectS的源代码复制到项目中?

java - 在测试类中模拟私有(private)静态最终变量

java - 使用 tempus-fugit 和 junit 时如何定义线程数?

java - 在 jenkins 中运行 maven/surefire 构建时间歇性 NoClassDefFoundError

编译器随机执行if语句

C文件指针读写问题

python - 在文本文件中删除和插入行

java - 是否可以将对象以人类可读的格式发送到文件?