Debugging Java Applications With IntelliJ IDEA Professional

I graduated from University of Texas with a BS in Computer Science in December of 2015 and I’ve been working for the University ever since. One of the problems I’ve encountered throughout my 3 year study is the lack of information on debugging applications. Sure you can get some fancy theoretical course on Debugging Programs, but majority of the students go through all the Computer Science courses writing damn print statements. Print statements might have been fine in the early 2000s, but come on, it’s 2016+. Please see the attached typical program
Typical CS student application

Why is debugging important?

You can easily shove print statements into your own personal code, but what happens when all of a sudden you are using a library like Serenity, JUnit, Hadoop, OpenNLP, or one of the thousands in the Maven repository? What if it’s some other developers code that has no instructions and looks like Javascript golf . Well you are in freaking trouble!

On the other hand, even if it is your own code? Do you have to compile and run the application everytime you add a print statement? Well what if you could debug in real time without ever having to recompile? That’s the ultimate benefit, I’m about to show you how to save an infinite amount of time in your finite lifetime.

Installing IntelliJ IDEA

Well it sounds expensive, but luckily enough it’s free for students. So setup your free account and download it today! It takes about 35 seconds to setup your account. PS. I wish I was an JetBrains affiliate I’d be making millions in endorsements 😀

So you downloaded the app drag it into the applications folder like every other installation. Run the application and when it asks about the 30 day trial, just click “Jetbrains account” and login with your credentials.

On the next screen go cool with the “Dracula” theme and click through the rest of the screens.

Finally it’ll ask “Create New Project” and select “Checkout from Version Control” Because we are hardcore and diving staight into the ocean. Clone one of my heroic repos with the URL: https://github.com/marek5050/selenium-serenity-jbehave-base. It’ll ask if you would like to open, click “Heck Yes!”

Setting up your first project

So one of the main things IntelliJ will always ask is for the location of your JDK. Which can be tricky… But to set it up go to File -> Project Structure -> Project-> Project SDK . The rest is up to you….

title
title

Well lucky you there’s no more configuration to do!

Information about the program

The program we have is a quality assurance browser automation tool I’ve been working with for the last couple of months. The whole point is to automate a users behaviour on a website. It’ll hit a couple of different websites on the internet such as Github, Twitter, IBM, and Crossfit.

The website we’ll actually be using for testing will be Crossfit. So find the test file in
src/test/social/Crossfit.java, you can also search for the file by pressing the Shift key twice.
title

When you open the file there will be a green arrow pointing at public void …
Green arrow pointing

Clicking the arrow unveils a new Run menu, here we have Run, Debug, Run with Coverage. Select Run just to see the behavior of the application.
title

On the bottom status bar you’ll see the program compiling and executing.
title title

So as long as nobody DDOS’s my server it’ll all be nice and green.
title

It also shows you the steps the test took.
crossfitDefinitions.Given the user accesses the crossfit.social landing page
crossfitDefinitions.When they select Athletes
crossfitDefinitions.Then we should see a list of athletes

Also notice running the application created a temporary Configuration profile in the top right corner.
Configuration profile

Setting the first breakpoints

So let’s find the third step the test executed.
“crossfitDefinitions.Then we should see a list of athletes”

Press shift twice and search for crossfitDefinitions. It’ll take you to CrossfitDefinitions.java
title

Here we have a sexy assert statement that currently passes, for fun and giggles set the value to greaterThan(100).

Re-run the program by pressing either the green arrow on the bottom bar or in the top right. It’ll recompile and execute again, but this time the assert will fail. Because the website only has 20 athletes per page.

Checking the Run log we’ll see the assertFailed with

java.lang.AssertionError: 
Expected: a value greater than <100>
     but: <20> was less than <100>

title

Actually setting the first breakpoint

We have got to the point in the application where something is broken. We might know now, but usually you’ll have no idea 🙂 So let’s pretend.

Let’s set a breakpoint on the line with the assert statement by clicking in the shaded area.
title
and now press the deBug button.
title

The application will run as normal until it hits the line. And BOOM!

Now we are in Debug mode.
title

There’s a couple of places to pay attention:
On the bottom you have the debugging panels.
Left side will have Debugger and Console
* Console – (in case you need those stupid print statements)
* Left side Debugger contains the call stack, you can actually move up and down the call stack and inspect the local variables throughout.
* Middle panel will have local variables.
* Right hand side will have the variables you are currently watching.
* Then we have the typcal jump over, step into, force step into.
* MAGIC BUTTON This is where you make the big money
I would love to jump straight into the MAGIC BUTTON but you all aren’t ready yet.

So in the middle panel we have all the variables and objects. You can inspect these objects and change their value.
title

Ok cool, now you are ready.
MAGIC BUTTON
The MAGIC BUTTON aka expression evaluator is the java version of a MacBook Pro with Retina display and 160gb of RAM. 😛 So click on it now, a dialog pops up.
Sweet I’ve seen a million dialogs before.
So inside the expression evaluator we can
Execute a command like assertThat(cs.getAthleteCount(),greaterThan(100))
title
Copy and Paste a command like cs.getAthleteCount()
title
Evaluate an expression like cs
title
Or even write a for loop by switching to Code Fragment:
int b=0;
int c = cs.getAthleteCount();
for( int a=0; a < c; a++){
b+=a;
}

title

Please please remember, we’re doing all of this WITHOUT RECOMPILING AND RERUNING! THIS IS ALL AT RUNTIME!

The framework we are using disconnects after about 2 minutes in debug mode. So if the evaluator seems like it’s hanging, just rerun the debugger by pressing Rerun.

Break on Exceptions

Cool, so what? I can do all that using print statements.
Okay stop the Debugger by pressing the Square.
And insert this into the list_of_athletes() function:

 int b=0;
 int c = cs.getAthleteCount();
 int[] d = new int[3];
 for( int a=0; a < c; a++){
 b+=d[a];
 }

Also put a breakpoint on the int b=0;
title

and press run. The run will throw an ArrayIndexOutOfBoundsException.
Darn we can’t change anything without breaking the application! Whats wrong now? Well we can actually break on exceptions by going into the Debug menu and clicking the two red circles.
title
We can set it to Any exception or some specific Exception like ArrayIndexOutOfBoundsException
title
title
title

Add the Exception breakpoint, make sure you select “Disabled until selected breakpoint is hit” select Line 32, select “leave enabled” and ensure both “Caught exception” and “Uncaught exception” are checked.
title
Then finally press the Debug button again.
Now it’ll break at b=0; press Resume.
After a little bit the program should break again finally on the line that throws the exception.
title

In the middle panel we can see that:
c=20
d=int[3] = {0,0,0}
a=3
so
d[3] is invalid and outOfBounds.
Invalid array range: 3 to 3.

So we know exactly what to fix. We could even open up the Expression evaluator and do a more in depth investigation of the variable environment.

Tips and Tricks

Conclusion

Finding a more efficient debugging mechanism will dramatically increase your productivity and greatly decreases the amount of time you spend debugging mistakes in your application. You can use an identical approach for debugging all kind of programming languages including Python, C, C++, Scala, or even Javascript and Node.JS.

Some other links

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s