Checked Exceptions


Each course session at the university will have its own web page. After creating a Session object, you can send it a String representing the URL[2] of its web page. Session should create a java.net.URL object using this string. One of the constructors of java.net.URL takes a properly formed URL string. To be properly formed, a URL must follow a number of rules for its component parts, such as the protocol name and host name. If you create a java.net.URL with an improperly formed URL, the constructor of java.net.URL throws an exception of type java.net.Malformed-URLException.

[2] Uniform Resource Locator, a pointer to a resource on the World Wide Web.


A simple test in SessionTest sets the URL into the session as a string. The test then retrieves the URL as a java.net.URL object. Finally, the test ensures that the URL object's string representation (toString) is a match for the URL string argument.

 public void testSessionUrl() {    final String url = "http://course.langrsoft.com/cmsc300";    session.setUrl(url);    assertEquals(url, session.getUrl().toString()); } 

The code in Session is just a setter and getter:

 package sis.studentinfo; import java.util.*; import java.net.*; abstract public class Session       implements Comparable<Session>, Iterable<Student> {    ...    private URL url;    ...    public void setUrl(String urlString) {       this.url = new URL(urlString);    }    public URL getUrl() {       return url;    }    ... 

When you compile, however, you will receive an error:

 unreported exception java.net.MalformedURLException; must be caught or declared to be thrown           this.url = new URL(urlString);                          ^ 

Java defines the exception type MalformedURLException as a checked exception (as opposed to an unchecked exception). A checked exception is an exception that you must explicitly deal with in your code. You may ignore an unchecked exception (such as NumberFormatException), but remember that ignoring an exception may not be safe.

Any code that can generate a checked exception must be handled by the method in which the code appears. You handle an exception either by using a try-catch block or by declaring that the method simply propagates the exception to any of its callers. To declare that the method propagates the exception (and thus otherwise ignores it), use the throws clause:

 public void setUrl(String urlString) throws MalformedURLException {    this.url = new URL(urlString); } 

The code phrase tHRows MalformedURLException says that the method setUrl can possibly generate an exception. Any code that calls the setUrl method must either be enclosed in a try-catch block or it must similarly appear in a method that contains an appropriate throws clause.

The test itself, then, must either catch MalformedURLException or declare that it throws MalformedURLException. Compile to see for yourself.

 public void testSessionUrl() throws MalformedURLException {    final String url = "http://course.langrsoft.com/cmsc300";    session.setUrl(url);    assertEquals(url, session.getUrl().toString()); } 

(Don't forget to add the java.net import statement to SessionTest.)

Unless you are specifically expecting to receive an exception as part of your test, simply declare that the test tHRows an exception. Do not enclose the setUrl call in a TRy-catch block. You are defining the test. For this test, you are defining a "happy case" in which you know that the URL is valid. Given these two things under your control, an exception should never be thrown during the execution of the test unless something is horribly wrong. You can safely ignore any exceptions for the purpose of such a positive test. In the unlikely event that an exception is thrown, JUnit will catch it and fail the test with an error.

You still need a negative test, one that demonstrates what the client will see when there is a problem:

 public void testInvalidSessionUrl() {    final String url = "httsp://course.langrsoft.com/cmsc300";    try {       session.setUrl(url);       fail("expected exception due to invalid protocol in URL");    }    catch (MalformedURLException success) {    } } 

You will need to import the MalformedURLException class.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net