Exercises


  1. Write a test that calls a method named blowsUp. Code the method blowsUp to throw a new RuntimeException with the message "Somebody should catch this!". Run the test. Verify that it fails with an appropriate stack trace.

  2. Make the test pass without removing the call to blowsUp. Ensure that the test fails if blowsUp throws no exception.

  3. Ensure that the caught exception contains the proper message. Change the message, and see the test fail. Change it back in order to keep the tests passing.

  4. Create a test that calls a new method named rethrows. The rethrows method should call blowsUp and catch the exception. It should then wrap the exception in a new RuntimeException and throw it again. Use getCause to ensure that the caught exception contains the original exception.

  5. Modify the test to expect a new exception type, SimpleException, when calling the blowsUp method. SimpleException should extend RuntimeException.

  6. Which of the following test methods will not compile? Of those that compile, which will pass and which will fail?

     public void testExceptionOrder1() {    try {       blowsUp();       rethrows();       fail("no exception");    }    catch (SimpleException yours) {       fail("caught wrong exception");    }    catch (RuntimeException success) {    } } public void testExceptionOrder2() {    try {       rethrows();       blowsUp();       fail("no exception");    }    catch (SimpleException success) {    }    catch (RuntimeException failure) {       fail("caught wrong exception");    } } public void testExceptionOrder3() {    try {       blowsUp();       rethrows();       fail("no exception");    }    catch (RuntimeException success) {    }    catch (SimpleException yours) {       fail("caught wrong exception");    } } public void testExceptionOrder4() {    try {       blowsUp();       rethrows();       fail("no exception");    }    catch (RuntimeException fail) {       fail("exception unacceptable");    }    catch (SimpleException yours) {       fail("caught wrong exception");    }    finally {       return;    } } public void testExceptionOrder5() {    try {       blowsUp();       rethrows();       fail("no exception");    }    catch (SimpleException yours) {       fail("caught wrong exception");    }    catch (RuntimeException success) {    } } public void testExceptionOrder6() {    try {       rethrows();       blowsUp();       fail("no exception");    }    catch (SimpleException yours) {       fail("caught wrong exception");    }    catch (RuntimeException success) {    } } public void testExceptionOrder7() {    try {       rethrows();       blowsUp();       fail("no exception");    }    catch (SimpleException success) {    }    catch (RuntimeException fail) {       fail("caught wrong exception");    } } public void testErrorException1() {    try {       throw new RuntimeException("fail");    }    catch (Exception success) {    } } public void testErrorException2() {    try {       new Dyer();    }    catch (Exception success) {    } } public void testErrorException3() {    try {       new Dyer();    }    catch (Error success) {    } } public void testErrorException4() {    try {       new Dyer();    }    catch (Throwable success) {    } } public void testErrorException5() {    try {       new Dyer();    }    catch (Throwable fail) {       fail("caught exception in wrong place");    }    catch (Error success) {    } } public void testErrorException6() {    try {       new Dyer();    }    catch (Error fail) {       fail("caught exception in wrong place");    }    catch (Throwable success) {    } } public void testErrorException7() {    try {       new Dyer();    }    catch (Error fail) {       fail("caught exception in wrong place");    }    catch (Throwable success) {    }    finally {       return;    } } Dyer: class Dyer {    Dyer() {       throw new RuntimeException("oops.");    } } 

  7. What compiler error would you expect the following code to generate? Copy it into a test class of your own and make it pass.

     public void testWithProblems() {    try {       doSomething();       fail("no exception");    }    catch (Exception success) {} } void doSomething() {    throw new Exception("blah"); } 

  8. What is wrong with the following commonly seen code?

     public void doSomething() {    try {       complexOperationWithSideEffects();    } catch (Exception e) {       e.printStackTrace();    } } 

  9. Write a method to log an exception to a given log with the stack trace in reverse order (so that the point of failure is at the tail of the log instead of the head).

  10. Create a custom Handler which discards the messages it receives and counts the number of times that messages were logged at various levels of severity. Use a map to store the count by severity.

  11. Create a custom log message formatter that you can optionally construct with a CountingLogHandler. If no CountingLogHandler is passed in, the formatter should produce output in the form:

     LEVEL: message 

    For example:

     WARNING: watch out 

    If a CountingLogHandler is passed in, each message should show the count for the current level. For example:

     WARNING: watch out (WARNING total = 1) 

    Make sure you write tests for both circumstances.

    Then ensure that the CountingLogHandler uses the custom formatter as its default. Modify the CountingLogHandler to store formatted output in a StringBuilder so that your test can request the complete logging summary.

    Finally, edit the logging property file and assign the custom formatter to the ConsoleHandler. Visually ensure that logging messages sent to the ConsoleHandler come out as expected.



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