Created
August 15, 2013 05:26
-
-
Save faloi/6238474 to your computer and use it in GitHub Desktop.
Ejemplo del uso de properties en groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Perro { | |
def nombre | |
def edad | |
def getSaludo() { | |
"Hola soy ${nombre}!" | |
} | |
def setEdad(nuevaEdad) { | |
if (nuevaEdad > 100) | |
throw new RuntimeException("Un perro no puede vivir mas de 100 anios!") | |
edad = nuevaEdad | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test | |
class PerroTest { | |
@Test | |
void "los atributos autogeneran getters"() { | |
def pluto = new Perro(nombre: "Pluto") | |
assert pluto.nombre == "Pluto" | |
assert pluto.getNombre() == "Pluto" | |
} | |
@Test | |
void "los atributos autogeneran setters"() { | |
def goofy = new Perro() | |
goofy.nombre = "Goofy" | |
assert goofy.nombre == "Goofy" | |
goofy.setNombre("Goofy el perro loco") | |
assert goofy.nombre == "Goofy el perro loco" | |
} | |
@Test | |
void "los getters pueden ser llamados como si fueran atributos"() { | |
def coraje = new Perro(nombre: "Coraje") | |
assert coraje.saludo == "Hola soy Coraje!" | |
} | |
@Test(expected=RuntimeException.class) | |
void "los setters pueden ser llamados como si fueran atributos"() { | |
def perroTrucho = new Perro() | |
perroTrucho.edad = 101 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muy copado el manejo de los constructores (Y)!
La semana que viene me pongo a hacer algunos refactors para sacar los constructores y hacer esos llamados mas bonitos jaja.