Chapter 3. Testing and Debugging
Testing and debugging are a way of life in Java development. Eclipse comes with built-in facilities for testing your code using its JUnit framework and some truly exceptional debugging capabilities.
Testing with JUnit
JUnit is an open source testing framework that comes with Eclipse. You can create JUnit-based classes in the same project as other classes, and use this JUnit code to test the other classes in your project. Using JUnit in this way, you can construct a set of standard tests for everyone working on an application, and if they change the application’s code, all they’ll need is a few clicks to verify that the application still passes the standard set of tests.
JUnit is designed to test your code, and it’s made up of assertion methods that can test various conditions. Here they are:
assertEquals(a, b)
Tests if
a
is equal tob
(a
andb
are either primitive values or must have anequals
method for comparison purposes)assertFalse(a)
Tests if
a
is false, wherea
is a Boolean valueassertNotNull(a)
Tests if
a
is notnull
, wherea
is either an object ornull
assertNotSame(a, b)
Tests if
a
andb
both do not refer to the identical objectassertNull(a)
Tests if
a
isnull
, wherea
is either an object ornull
assertSame(a, b)
Tests if
a
andb
both refer to the identical objectassertTrue(a)
Tests if
a
is true, wherea
is a Boolean value
You construct JUnit tests using these methods; when you run a JUnit application, it opens its own view to give you an ...
Get Eclipse 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.