Java Reference
In-Depth Information
With JUnit, you write a set of tests, called a suite, that create the Java objects
you've developed and call their methods. The values produced by these tests are
checked to see whether they're what you expected. All tests must pass for your
software to pass.
Although unit testing's only as good as the tests you create, the existence of a test
suite is extremely helpful when you make changes to your software. By running
the tests again after the changes, you can better assure yourself that it continues to
work correctly.
Most Java programmers prefer unit testing to assertions. Some even write tests
before they write any code.
Q Is there any way to get around the strict restrictions placed on methods by the
throws clause?
A Yes. Suppose that you have thought long and hard and have decided that you need
to circumvent this restriction. This is almost never the case because the right solu-
tion is to go back and redesign your methods to reflect the exceptions that you
need to throw. Imagine, however, that for some reason a system class has you in a
bind. Your first solution is to subclass RuntimeException to make up a new,
unchecked exception of your own. Now you can throw it to your heart's content
because the throws clause that was annoying you does not need to include this new
exception. If you need a lot of such exceptions, an elegant approach is to mix in
some novel exception interfaces with your new Runtime classes. You're free to
choose whatever subset of these new interfaces you want to catch (none of the
normal Runtime exceptions need to be caught), whereas any leftover Runtime
exceptions are allowed to go through that otherwise annoying standard method in
the library.
Quiz
Review today's material by taking this three-question quiz.
Questions
1. What keyword is used to jump out of a try block and into a finally block?
a. catch
b. return
c. while
 
Search WWH ::




Custom Search