正逢中秋国庆,祝各位大牛们双节快乐!
分享一下自己提高工作效率常用的 IDEA Debug 技巧
示例方法
public List<StudentScore> getStudentScore() {
System.out.println("do getStudentScore~");
return Lists.newArrayList(
new StudentScore("小张", "语文", 90),
new StudentScore("小李", "语文", 81),
new StudentScore("小王", "语文", 98));
}
1.带条件断点
在循环体中调试代码时,想对指定值进行调试,就可以使用条件断点,只有满足Condition 时,断点才会生效;
/**
* 条件断点
*/
@Test
public void test1() {
List<StudentScore> studentScore = this.getStudentScore();
for (StudentScore score : studentScore) {
System.out.println("小李断点测试:" + JSONObject.toJSONString(score));
}
}
2.断点回退
在Debug调试过程中,如果需要退回到已执行的代码行时,可以在Debug面板找到Drop Frame
(丢弃栈帧) 或者 Reset Frame
(重置栈帧,回到前一帧),这里需要注意,Reset Frame
是新版本IDEA才有的功能,老版本看不到这个功能项;
3.执行表达式
在Debug过程中,当方法中执行对象的参数,不能满足本次执行的 Mock 数据时,我们可以直接通过自定义表达式来达到效果;
例如:在生产环境捞出的日志报文 JSON 对象,在测试环境单次 Mock 时并没有小田的信息,而我们需要看到包含“小田”对象的执行结果,这时就可以使用 Evaluate Expression
达到目标效果执行方法;
{
"name":"小田","score":80,"subject":"数据"}
使用 Evaluate Expression
:
4.终止执行
执行到某行代码时,需要终止执行,不在继续执行后面的逻辑,可以使用 Force Return
;(tips:个人感觉这个很有用,因为就算停止程序运行,断点后续代码还是会继续执行,继续执行原因嘛,因为进了方法栈,肯定需要出栈,所以方法还是会执行完)
/**
* 终止执行
*/
@Test
public void test4() {
List<StudentScore> studentScore = this.getStudentScore();
System.out.println("do Force Return");
System.out.println("小李断点测试:" + JSONObject.toJSONString(studentScore));
}
操作完成 Force Return
后查看控制台输出(没有输出 “do Force Return”):
5.常用功能
常用功能说明:
Show Execution Point
(alt + F10):回到当前断点停顿处;Step Over
(F8):断点至当前行的后一行(方法行不会进入方法);Step Info
(F7):方法行会进去方法,非方法行会执行下一行;Force Step Into
(alt + shift + F7):可以直接进入源码方法行;Step Info
就无法直接进入源码方法行;Step Out
(shift + F8):返回到Step Info
入口行的下一行;Drop Frame
:丢弃栈帧;Run to Cursor
(alt + F9):断点直接跳转鼠标光标所在行;
感 谢
各 位
大 佬
的 阅
读,随
手 点
赞,日
薪 过
万~!
!!