工作记录------判空
List
背景:测试人员在测试接口时,对报文中一个为数组类型的字段,传入[]。
如图:
"scheduleCount": []
而在代码中对这个数组对象进行了是否为null的判断,但是竟然没有校验主,也就是说这个数组不为null。
示例代码:是否为null?,以及数组长度?。
@Test
public void test(){
List<Integer> scheduleCount = new ArrayList<>();
System.out.println(scheduleCount == null);//true
System.out.println(scheduleCount.size()); //0
}
打印结果为true、和0。
接口调用时,尽管传过来的是空数组,但还是分配了内存空间。因此没有校验主。
除非不传这个字段,或者这样传
"scheduleCount": "",
但是这样传,scheduleCount就为String对象,反而找不到这个接口。
经过查询,其实通过CollectionUtils工具类能够直接判断,推荐使用
CollectionUtils.isEmpty()
Integer
背景:同样是这个接口,测试人员传的空值,结果竟然又没有校验住
"accountType":"",
经过查询代码是这样进行校验:
StringUtils.isEmpty(String.valueOf(infoDto.getAccountType()))
经过分析
infoDto.getAccountType;//返回值是null
String.valueOf(infoDto.getAccountType());//打印结果竟然是字符串null
String.valueOf()会将为null的Integer类型,输出为null字符串。