Chapter 5. Testing and Debugging

Introduction

Testing and debugging code is a fact of life for Java developers, and this chapter focuses on the testing and debugging facilities that come with Eclipse. Often, you can test your code using the JUnit package that comes with Eclipse, and you can debug it using the built-in debugger. We’ll look at both facilities in this chapter.

JUnit is an open source testing framework (for more information on the Open Source Initiative, visit http://www.opensource.org). You use it to test the results of JUnit-based classes that call methods in your code. An Eclipse plug-in facilitates the process of running JUnit tests. Using JUnit, you can create a standard set of tests and distribute them to everyone working on your code; if someone edits your code, all that person needs to do is to run the tests with a few mouse clicks to make sure he didn’t break anything.

JUnit works by calling your code using a set of assertion methods that test the return values from your code. Here are the JUnit tests:

assertEquals(a, b)

Tests if a is equal to b (a and b are primitive values or must have an equals method for comparison purposes)

assertFalse(a)

Tests if a is false, where a is a Boolean value

assertNotNull(a)

Tests if a is not null, where a is either an object or null

assertNotSame(a, b)

Tests if both a and b do not refer to the same object

assertNull(a)

Tests if a is null, where a is either an object or null

assertSame(a, b)

Tests if both a and b refer to the ...

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.