It’s now time to debug what went wrong. For that, we can use the debugger. Find the line of the assertion that failed. Double click on the dot next to the line number. This will cause a small red dot to appear to the left of the line number. This indicates a breakpoint
. When you run the code in a debugger, each time a breakpoint is reached, the debugger will stop and give you control.
From the run test menu, click the triangle icon with a bug on it, to the right of the regular run icon.
This will run until your code reaches the line with the breakpoint on it. You will see this menu above your code.
Each of these icons will do something useful for you while debugging. The triangle will simply run your code to the next breakpoint (or the end of your code). The arrow curving over the dot (Step Over) will take you to the next line of code. The arrow pointing at the dot (Step Into) will take you into a method. The arrow pointing away from the dot will take you to the method that called whatever method you’re currently in. The green circular arrow will restart your code, and the red box will stop it.
Click the Step Into
button to step into the method that failed the assertion. Step through the method using Step Over
. After each step, notice that the line of code you just ran is now annotated with the values that were just generated. Also notice that on the left hand side of VS code, there’s a list of all your variables - click on these variable names to see their values. Keep clicking until you see what the current values inside the ArrayList
inside DataSet
are.
Once you have figured out the bug, you can stop debugging by clicking the red box. Fix the bug and rerun your tests. Hopefully, it’s passing now. If not, start the debugger again and give it another go.
Debugging is an iterative process. It’s not uncommon to need to spend more time debugging code than writing code. While using the VS Code debugger may initially seem overwhelming, getting good at using it will let you finish coding projects much faster. Using these tools is standard practice in the software industry.