Created
March 29, 2015 15:06
-
-
Save heat/b1905a2ebd697cb5ea76 to your computer and use it in GitHub Desktop.
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
package entities; | |
public class SubClass extends SuperClass { | |
private int attribute; | |
public int getAttribute() { | |
return attribute; | |
} | |
} |
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
package entities; | |
import java.lang.reflect.Field; | |
public class SuperClass { | |
public void __set(String field, Object value){ | |
try { | |
Field f = getClass().getDeclaredField(field); | |
f.setAccessible(true); | |
f.set(this, value); | |
} catch (NoSuchFieldException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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 entities.SubClass | |
import org.specs2.mutable._ | |
class ReflectionClass extends Specification{ | |
"Uma classe com estado" should { | |
"poder definir um estado " in { | |
val result: String = "teste" | |
result must_==("teste") | |
} | |
} | |
"Uma super classe " should { | |
"alterar atributos da sublclasse SubClass com _set " in { | |
val expected = 1 | |
val subClass = new SubClass() | |
subClass.__set("attribute", expected) | |
val result = subClass.getAttribute | |
result must_==(expected) | |
} | |
"Criar uma instancia de sua subclasse com __empty " in { | |
val expected = 0 | |
val subClass : SubClass = SubClass.__empty() | |
val result = subClass.getAttribute | |
result must_==(expected) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment