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)

JUnit testing (Simple)


JUnit is a testing framework written specifically for Java. It allows the user to test each method one by one and makes sure that code refactoring or improvements to your project does not break the other classes, dependent methods, or subsystems. For example, you have several classes dependent on each other and you need to ensure that any changes that you are making to one class won't break the other classes. JUnit tests provide this functionality, allowing you to make sure that your changes do not break the system. Often, projects include JUnit tests in their build process and do not approve the built result unless all JUnit tests pass. Moreover, JUnit tests not only make your programs more stable, but also save you a lot of time.

How to do it...

There is one more big advantage of being an Eclipse user in terms of JUnit tests. In all the new versions of Eclipse with JDT, the JUnit framework comes prepackaged, so you don't need to download and install it.

To create a JUnit test case, execute the following steps:

  1. Go to File | New | Other… | Java | JUnit | JUnitTest Case as shown in the following screenshot:

  2. Click on Next>.

  3. In the Package field enter how.to.eclipse.tests.

  4. In the Name field enter EmployeeTests.

  5. Click on Finish. (If you are asked to add JUnit 4 to build a path, click on Ok.)

If you have done everything correctly, you should see a couple of new things in your Package Explorer.

  1. Go to Java Perspective if you are not already there. See the following screenshot if you forgot how to switch perspectives:

  2. In Package Explorer expand our how-to-debug project.

    You should see a couple of new things. First of all, under src | how.to.eclipse.tests there is a new ExmployeeTests class. This is our test class, which we will be using to put our tests for the Employee class. Secondly, there is a JUnit 4 library added to our build path, so that we are able to run JUnit tests.

    If we open the EmployeeTests class, we will see that there is one test() method with a method call fail() in it.

  3. Click the icon (Run).

After running the EmployeeTests method you should see the JUnit tests' interface that looks like the following screenshot:

The JUnit interface is straightforward. On the top of the JUnit view is a menu that helps to navigate through the test classes. Below the menu is a time indicator, which shows how much time it took to run the test. Under the time indicator there is a test statistics and an indicator of whether the tests have passed (green) or failed (red). Finally, below the red/green indicator there is a hierarchy of tests and Failure Trace. We will see how all these tools work when we start creating JUnit tests for our Employee class.

Meanwhile, if you get through all these steps, congratulations. You know how to set up JUnit tests, which, as I have already mentioned, is a very straightforward process.