运用表达式引擎测试bean的字段
使用表达式引擎测试bean的字段
今天做梦梦见和人讨论上个项目的技术实现,突然灵感来了,想到如何简化测试用例的书写.醒来后简单实现了一个,仅供大家参考. 我很懒,一般都不写注释,大家凑合着看看吧:(
概要:
把
简化为:
或者
或者
或者
ELAssert.java 基本实现类
TestBean.java 测试中使用到的bean
ELAssertTest,java 简单的测试用例
已知问题:
1.此实现使用的是nutz的el实现,但是貌似这个实现对float和long的支持有点问题,注释部分不能通过,这个问题我已经到google code上提交了一个issue.
2.json不支持char,和byte,所以暂时没考虑这两种类型的处理
今天做梦梦见和人讨论上个项目的技术实现,突然灵感来了,想到如何简化测试用例的书写.醒来后简单实现了一个,仅供大家参考. 我很懒,一般都不写注释,大家凑合着看看吧:(
概要:
把
Assert.assertEquals(1, testBean.get_int()); Assert.assertEquals(true, testBean.is_bool()); Assert.assertEquals(1.23, testBean.get_double(), 10); Assert.assertEquals(1.2f, testBean.get_float(), 10); Assert.assertEquals('c', testBean.get_char()); Assert.assertEquals(1234l, testBean.get_long()); Assert.assertEquals((byte) 0x01, testBean.get_byte()); Assert.assertEquals("abc", testBean.get_string());
简化为:
ELAssert.assertTrue( testBean, "this._int==1 && this._bool==true && this._double==1.23 && this._string=='abc'");
或者
ELAssert.assertJsonFields(testBean, "_int:1,_bool:true,_double:1.23,_float:1.2,_long:1234,_string:'abc'");
或者
ELAssert.assertFieldMap(testBean, Lang.map("{_int:1,_bool:true,_double:1.23,_float:1.2,_long:1234,_string:'abc'}"));
或者
ELAssert.assertFields(testBean, "_int,_bool,_double,_float,_long,_string", 1, true, 1.23, 1.2f, 1234l, "abc");
ELAssert.java 基本实现类
package com.watano.util.test; import java.util.Map; import junit.framework.Assert; import org.apache.commons.lang.StringUtils; import org.nutz.el.El; import org.nutz.lang.Lang; import org.nutz.lang.util.Context; public class ELAssert { public static void assertTrue(Object actual, String expected) { Context context = Lang.context(); context.set("this", actual); Boolean iactual = (Boolean) El.eval(context, expected); Assert.assertTrue("Expected: " + expected + " but was: " + iactual, iactual); } public static void assertJsonFields(Object actual, String expectedjson) { assertFieldMap(actual, Lang.map(expectedjson)); } public static void assertFieldMap(Object actual, Map<String, Object> expected) { Context context = Lang.context(); context.set("this", actual); for (String field : expected.keySet()) { Object iactual = El.eval(context, "this." + field); Object iexpected = expected.get(field); if (iactual instanceof Float || iactual instanceof Long) { iactual = iactual + ""; iexpected = iexpected + ""; } Assert.assertEquals("Field '" + field + "' Expected: " + iexpected + " but was: " + iactual, iexpected, iactual); } } public static void assertFields(Object actual, String fields, Object... expecteds) { Context context = Lang.context(); context.set("this", actual); String[] arrFields = StringUtils.split(fields, ','); for (int i = 0; i < arrFields.length; i++) { String field = arrFields[i].trim(); Object iactual = El.eval(context, "this." + field); Object iexpected = expecteds[i++]; if (iexpected == null) { Assert.assertNull("Field '" + field + "' Expected: " + iexpected + " but was: " + iactual, iactual); } else { Assert.assertEquals("Field '" + field + "' Expected: " + iexpected + " but was: " + iactual, iexpected, iactual); } } } }
TestBean.java 测试中使用到的bean
package com.watano.util.test; public class TestBean { private int _int; private boolean _bool; private double _double; private float _float; private char _char; private long _long; private byte _byte; private String _string; public TestBean(int _int, boolean _bool, double _double, float _float, char _char, long _long, byte _byte, String _string) { super(); this._int = _int; this._bool = _bool; this._double = _double; this._float = _float; this._char = _char; this._long = _long; this._byte = _byte; this._string = _string; } public int get_int() { return _int; } public void set_int(int _int) { this._int = _int; } public boolean is_bool() { return _bool; } public void set_bool(boolean _bool) { this._bool = _bool; } public double get_double() { return _double; } public void set_double(double _double) { this._double = _double; } public float get_float() { return _float; } public void set_float(float _float) { this._float = _float; } public char get_char() { return _char; } public void set_char(char _char) { this._char = _char; } public long get_long() { return _long; } public void set_long(long _long) { this._long = _long; } public byte get_byte() { return _byte; } public void set_byte(byte _byte) { this._byte = _byte; } public String get_string() { return _string; } public void set_string(String _string) { this._string = _string; } }
ELAssertTest,java 简单的测试用例
package com.watano.util.test; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.nutz.el.El; import org.nutz.lang.Lang; import org.nutz.lang.util.Context; public class ELAssertTest { private TestBean testBean; @Before public void setUp() throws Exception { // int _int, boolean _bool, double _double, float _float, char _char, long _long, byte _byte, String _string testBean = new TestBean(1, true, 1.23, 1.2f, 'c', 1234l, (byte) 0x01, "abc"); } @Test public void testAssertTrue() { Context context = Lang.context(); context.set("a", 1.2); Assert.assertTrue((Boolean) El.eval(context, "a==1.2")); // context.set("a", 1.2f); // Assert.assertTrue((Boolean) El.eval(context, "a==1.2")); // context.set("a", 123l); // Assert.assertTrue((Boolean) El.eval(context, "a==123")); ELAssert.assertTrue( testBean, "this._double==1.23"); // ELAssert.assertTrue( // testBean, // "this._float==1.2"); // ELAssert.assertTrue( // testBean, // "this._long==1234"); ELAssert.assertTrue( testBean, "this._int==1 && this._bool==true && this._double==1.23 && this._string=='abc'"); } @Test public void testAssertJsonFields() { ELAssert.assertJsonFields(testBean, "_int:1,_bool:true,_double:1.23,_float:1.2,_long:1234,_string:'abc'"); } @Test public void testAssertFieldMap() { ELAssert.assertFieldMap(testBean, Lang.map("{_int:1,_bool:true,_double:1.23,_float:1.2,_long:1234,_string:'abc'}")); } @Test public void testAssertFields() { ELAssert.assertFields(testBean, "_int,_bool,_double,_float,_long,_string", 1, true, 1.23, 1.2f, 1234l, "abc"); } }
已知问题:
1.此实现使用的是nutz的el实现,但是貌似这个实现对float和long的支持有点问题,注释部分不能通过,这个问题我已经到google code上提交了一个issue.
2.json不支持char,和byte,所以暂时没考虑这两种类型的处理
1 楼
饶首建
2011-10-07
这个还可以撒
2 楼
watanoeye
2011-10-08
其实这个东西很简单,只不过这个居然是我在梦中想到的完整实现方案,这就有点神奇了,不知道大家也有类似的经验吗?