java - Android unit test for Enum type -
i came across strange. example of function:
public class f { public enum pawn {black, white} public pawn x; public f(){ this.x = pawn.black; } public pawn test(){ return this.x; } }
unit test this:
import junit.framework.assert; import junit.framework.testcase; import <package ... >.f; public class ftest extends testcase { public void testvalue(){ f mf = new f(); assert.assertsame(pawn.black, mf.test()); } }
and junit output:
junit.framework.assertionfailederror: expected same:<black> not:<black> expected :black actual :black
black not black , black. it's black magic. :/ can make work (i.e. pass test)?
you need assertequals, not assertsame
assertequals(java.lang.object expected, java.lang.object actual) asserts 2 objects equal.
assertsame(java.lang.object expected, java.lang.object actual) asserts 2 objects refer same object
your code comparing object f.x against enum object pawn.black. should be
public void testvalue(){ f mf = new f(); assert.assertequals(f.pawn.black, mf.test()); }
Comments
Post a Comment