我是Groovy的新手,但我却无法解决这个问题。感谢任何帮助。
我想从Groovy中读取一个文件。在读取内容时,对于每一行,我想用不同的字符串值替换字符串“$ {random_id}”和“$ {entryAuthor}”。
protected def doPost(String url, URL bodyFile, Map headers = new HashMap() ) {
StringBuffer sb = new StringBuffer()
def randomId = getRandomId()
bodyFile.eachLine { line ->
sb.append( line.replace("\u0024\u007Brandom_id\u007D", randomId)
.replace("\u0024\u007BentryAuthor\u007D", entryAuthor) )
sb.append("\n")
}
return doPost(url, sb.toString())
}
但我遇到了以下错误:
groovy.lang.MissingPropertyException:
No such property: random_id for class: tests.SimplePostTest
Possible solutions: randomId
at foo.test.framework.FooTest.doPost_closure1(FooTest.groovy:85)
at groovy.lang.Closure.call(Closure.java:411)
at groovy.lang.Closure.call(Closure.java:427)
at foo.test.framework.FooTest.doPost(FooTest.groovy:83)
at foo.test.framework.FooTest.doPost(FooTest.groovy:80)
at tests.SimplePostTest.Post & check Entry ID(SimplePostTest.groovy:42)
为什么它会抱怨一个属性,当我什么也没做?我还尝试过在Groovy中使用Java String.replace()中的"\$\{random_id\}",但不起作用。
random_id
的属性,但我看到了一个名为randomId
的属性。确实缺少random_id
属性 :) - ubiquibacon