I m making a unit test that needs to return a Json. To build that json i m using toPrettyString() method from JSONBuilder.
This is the class to spec:
class Lugar {
String sigla
String nombre
Coordenada coordenada
String toString(){
"${sigla}"
}
String toJson()
{
new JsonBuilder( this ).toPrettyString()
}
static constraints = {
nombre blank: false , nullable: false
}
}
The spec to run is this:
@TestFor(Lugar)
class LugarSpec extends Specification {
void "toJson not empty"() {
when:
Lugar lugar = new Lugar(sigla: "BUE", nombre:"BUENOS AIRES")
String aux = lugar.toJson();
then:
! aux.dump().empty
}
}
But the result is:
<error type="java.lang.StackOverflowError">java.lang.StackOverflowError
at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
at groovy.lang.MetaBeanProperty.getProperty(MetaBeanProperty.java:60)
at groovy.lang.PropertyValue.getValue(PropertyValue.java:40)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:287)
at groovy.json.JsonOutput.writeMap(JsonOutput.java:421)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:291)
at groovy.json.JsonOutput.writeArray(JsonOutput.java:326)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:283)
at groovy.json.JsonOutput.writeMap(JsonOutput.java:421)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:291)
at groovy.json.JsonOutput.writeArray(JsonOutput.java:326)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:283)
at groovy.json.JsonOutput.writeMap(JsonOutput.java:421)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:291)
at groovy.json.JsonOutput.writeArray(JsonOutput.java:326)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:283)
at groovy.json.JsonOutput.writeMap(JsonOutput.java:421)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:291)
at groovy.json.JsonOutput.writeArray(JsonOutput.java:326)
at groovy.json.JsonOutput.writeObject(JsonOutput.java:283)
at groovy.json.JsonOutput.writeMap(JsonOutput.java:421)
And it continues repeating until the end.
I tried to test toJson on a main and the results were fine:
static void main(String[] args) {
Lugar lugar = new Lugar(sigla: "BUE", nombre:"BUENOS AIRES")
String aux = lugar.toJson();
println aux.dump()
}
The results were:
{
"sigla": "BUE",
"constraints": {
"nombre": {
"blank": false,
"nullable": false
}
},
"nombre": "BUENOS AIRES"
}
Well i'd really appreciate i m trying to make some TDD and i m new on Grails.
Thanks.
Aucun commentaire:
Enregistrer un commentaire