在我们做接口测试的时候通常会验证返回的实体类中的空字段。
但是如果一个个字段的
assertNotNull
,不仅影响其准确性,而且会延长编码时间和调试时间。
这里介绍一个小工具,它可以把我们频繁的
assertNotNull
转变成一句话,实现更强大的功能。
希望大家可以提出宝贵意见哦!!
方法如下:
public
static
String assertGetMethod(Object objectClass) {
String str =
“”
;
Object[] args =
new
Object[0];
Class itemClass = objectClass.getClass();
Method[] methods = itemClass.getMethods();
for
(Method method : methods) {
if
(method.getName().startsWith(
“get”
)) {
try
{
Object oa = method.invoke(objectClass, args);
if
(
null
!= oa) {
System.
out
.println(method.getName() +
“:”
+ oa.toString());
}
else
{
str+=method.getName().substring(3, method.getName().length())+
“,”
;
}
}
catch
(IllegalArgumentException e) {
e.printStackTrace();
}
catch
(IllegalAccessException e) {
e.printStackTrace();
}
catch
(InvocationTargetException e) {
e.printStackTrace();
}
}
}
return
(
“”
.equals(str)) ?
null
: (str.substring(0,str.length()-1));
}
这个工具是用反射法实现的,具体的实现我就不说了。主要介绍一下用法吧。
举个例子好了。
例如,这里有一个类
A
public
class
A {
String
a
;
String
b
;
String
c
;
public
String getA() {
return
a
;
}
public
void
setA(String a) {
this
.
a
= a;
}
public
String getB() {
return
b
;
}
public
void
setB(String b) {
this
.
b
= b;
}
public
String getC() {
return
c
;
}
public
void
setC(String c) {
this
.
c
= c;
}
}
—————————————————————–
工具说明:
public
static
void
main(String[] args) {
A a =
new
A();
System.
out
.println(assertGetMethod
(b));
}
打印出:
A,B
(为空的方法名)
public
static
void
main(String[] args) {
A b =
new
A();
b.setA(
“a”
);
b.setB(
“b”
);
b.setC(
“c”
);
System.
out
.println(assertGetMethod
(b));
}
打印出:
null
—————————————————————–
使用说明:
我们验证的时候,如果要求所有的字段不能为空。
那么只要
:
Assert.assertNull(
“
以下字段值为空:
n” +assertGetMethod
(b)”,
assertGetMethod
(b)
);
如果要求个别值可以为空
(
如
B
和
C
可以为空
)
,那么只要:
public
static
void
main(String[] args) {
A a
=
new
A();
a
.setA(
“a”
);
String[] nullNames = assertGetMethod
(a
).split(
“,”
);
String validName=
“”
;
for
(String string : nullNames) {
if
(!string.equals(
“B”
)
&& !string.equals(
“C”
)){
validName = string +
“,”
;
}
}
Assert.assertNull(
“
以下字段值为空:
n” + validName”,
validName
);
}
相关推荐
帮助解决网页和JS文件中的中文编码问题的小工具 慎用const关键字 装箱,拆箱以及反射 动态调用对象的属性和方法——性能和灵活性兼备的方法 消除由try/catch语句带来的warning 微软的应试题完整版(附答案) 一个...
在右窗格中,右键单击 Last Counter 或 Last Help,选择“修改”,单击 Base = Decimal,在“值”数据字段中设置值,再单击“确定”。如有必要,请对其他项重复此操作,然后关闭注册表编辑器。 再次运行 SQL Server...
说明: (如果值为 TRUE) 当在隐式转换中丢失数据时返回错误的参数。 值范围: FALSE | TRUE 默认值: TRUE nls_numeric_characters: 说明: 指定将用作组分隔符和小数位的字符。组分隔符就是用来分隔整数位组 (如千, ...
当你的应用存储数据到Session中,或从Session中清除数据,Servlet都会通过HttpSessionBindingListener检杳什么类被绑定或被取消绑定。这个接口的方法会通报被绑定或被取消绑定的对象。 绪言 这是一份关于2.1版...
用检验工具检查问题 39 比较你的EAR文件和样本EAR文件 39 其它异常 39 第二部分 EJB技术 40 第3章 企业Bean 40 1,企业Bean概述 41 企业Bean的优点 41 何时需要使用企业Bean 41 企业Bean的分类 41 2,会话Bean 42 ...
13.3.3 实体关系 13.3.4 查询存储过程 13.3.5 LINQ to Entities查询揭秘 13.4 数据库操作 13.4.1 插入 13.4.2 更新 13.4.3 删除 13.4.4 管理并发 13.4.5 处理并发冲突 13.5 EntityDataSource...
修改外部数据库在4.12版中导致的不兼容问题,并增加了对MS SQL Server数据库中image和text字段类型的说明。 7. 修改扩展界面支持库一,禁止透明标签在父窗口刷新时自动刷新,以解决其导致窗口刷新缓冲的问题。 8....
《ASP.NET 4高级程序设计(第4版)》是ASP.NET领域的鸿篇巨制,全面讲解了ASP.NET4的各种特性及其背后的工作原理,并给出了许多针对如何构建复杂、可扩展的网站从实践中得出的建议。书中还深入讲述了其他ASP.NET图书...
14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. ...
14.1.5. 返回非受管实体(Returning non-managed entities) 14.1.6. 处理继承(Handling inheritance) 14.1.7. 参数(Parameters) 14.2. 命名SQL查询 14.2.1. 使用return-property来明确地指定字段/别名 14.2.2. ...