Book Image

Instant Eclipse Application Testing How-to

By : Anatoly Spektor
Book Image

Instant Eclipse Application Testing How-to

By: Anatoly Spektor

Overview of this book

<p>Detecting bugs and flaws in an application is difficult. Eclipse is a multi-language software development environment comprising of an Integrated Development Environment (IDE) and an extensible plugin system. Testing the Eclipse Platform during every build using an extensive suite of automated tests helps in disclosing bugs and rectifying them.<br /><br />"Instant Eclipse Application Testing How-to" is a quick guide to learning how to test all types of Java applications in an Eclipse environment. This book gives you a step-by-step approach towards application testing and debugging along with optimized sample test projects.<br /><br />"Instant Eclipse Application Testing How-to" is a hands-on guide that gives developers an insight into how to test Java applications using Eclipse IDE. This book will guide you through the process by allowing you to create a Java application and debug it using a wide variety of Eclipse debugging tools. The book is filled with practical examples, so you will start coding and debugging right away. After reading the book you will be proficient enough to debug Java applications of any scope.</p>
Table of Contents (7 chapters)

Using a conditional breakpoint (Intermediate)


Sometimes we want our breakpoints to be triggered only under certain conditions. For example, you have a big application with plenty of classes, and when you run it, it gives you unexpected results. If you want to debug such a case, one of the options is to set a breakpoint and track every time certain variables are used, and check the Variables view for the values of these variables. It can take a lot of time, as one method can be called thousands of times. It would be much easier if you could determine the nature of these unexpected results, and trigger a breakpoint only if these results occur. Thus, you should use a conditional breakpoint.

Getting ready

To set up a conditional breakpoint, we will need to make some modifications to our sample program, but first you have to unset the breakpoint from the previous example.

When the breakpoint from the previous example is unset, we will modify our code by performing the following steps:

  1. We will be using the setPosition() function (on line 30). Let's comment out the if statement in this method. Thus we can set any position we like. The setPosition() function will look like the following:

    30. public void setPosition (String _position) {
    31.   // if ( Arrays.asList(validPositions).contains(_position)){
    32.     position = _position;
    33.   //} 
    34. }
  2. Go to the main() method and change line 112 to the following:

    andrew.setPosition("");

    It means that we are providing an empty string as a position.

How to do it...

  1. Set a regular breakpoint to the line 32 (position = _position).

    Tip

    Use Ctrl + L to quickly navigate to the line.

  2. Right-click on the breakpoint and select Breakpoint Properties…. (The last option as shown in the following screenshot.)

  3. In Breakpoint Properties… check the Enable Condition box and in the textbox below type _position.isEmpty().

    This means that the breakpoint will be triggered only if _position is an empty string. Note that, we assign __position, but not the class variable position, because at the time the conditional breakpoint is triggered, the position will not be assigned yet.

  4. Click on Ok.

Your breakpoint should now look like this icon. It means that the breakpoint is only triggered when the condition applies.

Now, as we have set up our conditional breakpoint, let's run the application.

  1. Run the debugger by clicking on the icon.

    If you did everything correctly, you should see that the application has stopped on line 32 and is waiting for your actions. It stopped because _position is empty. If _position were not empty, it would skip this breakpoint and proceed with executing the application. Thus, a conditional breakpoint allows us to trigger the breakpoints only for the cases when we really want the program to stop executing.

  2. Now to make the final change to our application so that we can see the difference, first please terminate by clicking on the icon.

  3. Go to line 112 and pass the word tester to setPosition(String). Type the following:

    andrew.setPosition("tester");
  4. Click on the Debug button again.

This was done to show you that when the position is not empty, the conditional breakpoint is simply skipped.

Now you know how to set up the conditional breakpoint. The condition could be anything you want. It all depends on a particular problem that you are trying to solve.