Testing is essential if you want to have any faith that your programs work.
In the past, we were typing test cases one by one into the CodePad. This works well if you only have a few test cases, and it's easy to tell by eye whether the answers are right. But once your programs get bigger and you have more test cases, it's a royal pain. Fortunately, if you've written your examples using JUnit or Tester, it's much easier.
BlueJ makes this extremely easy: hit the "Run Tests" button. It will look in all your test classes, run all the methods whose names start with "test", and show you a report of which test methods passed and which failed. Double-clicking on a failed method shows you which line of the source code triggered the failure.
This is very convenient, but it has at least two disadvantages: first, as
mentioned before, assertEquals
doesn't work very well on methods
that return complicated data types. Second, if a JUnit test method has a
dozen assertEquals
calls and the third one fails, it will never
go on to the fourth through twelfth.
To run tests with Tester, right-click on your BlahTest
class and choose the testEverything
method (which we wrote when
we first set up that class). It will run all the methods in this
class whose names start with "test", and show you a report of which
test methods passed and which failed. It doesn't hot-link to the source code,
but for each failed test, it tells you the line number in the source code, so
you can still find it.
This report is in the "Terminal Window". If the amount of output exceeds the size of the window, you may need to go to the "Options" menu (when the terminal window is in front) and choose "Unlimited Buffering" before running the tests.