Guide to Object-oriented Programming With Java (Buffalo State University Version)
Chapter 13: JUnit Testing and Using the Debug Tool
Testing an Application Using Junit
The eclipse IDE allows you to use Junit to create unit tests to test individual sections of yourr Java code.
A DistanceConverter.java
Example
Before you can write any unit tests you need to create the application to be tested. For example, DistanceConverter.java
Creating the Junit Test Template
Use the following steps to create a template Junit test for your DistanceConverter.java
file.
Adding Test Cases
Now you are ready to add some test cases to the DistanceConverterTest.java
file:
Now run
your DistanceConverterTest.java
file to produce the following window.
Note: the above example has a test failure because the test case for convertFeettoMeters()
is testing for feet to be a 2 but we assert
(expects) it to be a 1.
Using the Eclipse Debug Tool
Eclipse has a built-in debug tool that provides a way to walk through your code and observe values for your variables and objects. You can set breakpoints so that when you run your code Eclipse will stop at the next breakpoint. This is a handly tool to determine if your variables have a value that you expect them to have and to help figure out why you may be getting unexpected results.