The Artima Developer Community
Sponsored Link

Java Buzz Forum
Keyed Exceptions and Negative Test Cases

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Paul Brown

Posts: 284
Nickname: paulrbrown
Registered: Dec, 2003

Paul Brown is an entrepreneur
Keyed Exceptions and Negative Test Cases Posted: Jun 3, 2004 9:39 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Paul Brown.
Original Post: Keyed Exceptions and Negative Test Cases
Feed Title: mult.ifario.us
Feed URL: http://feeds.feedburner.com/MultifariousCategoryJava
Feed Description: Software. Business. Java. XML. Web Services.
Latest Java Buzz Posts
Latest Java Buzz Posts by Paul Brown
Latest Posts From mult.ifario.us

Advertisement
Good error messages are a cornerstone of any useable software tool, but automated testing of error messages can be a challenge. Using the exception message is not usually appropriate, as it can change through internationalization or an exception that only provides formatted messages.The way that I've attacked this sort of thing in the past is to use what could be called keyed exceptions. That is, a class that looks like:public class MyException extends Exception { public static final short STUPID_ERROR = 0; public short _key; public MyException(short key) { super(); _key = key; } public getKey() { return _key; } }with a few other constructors if need be (e.g., with a message or nested exception) and some additional keys. When the exception is constructed and thrown from code, it should be supplied with the appropriate key:throw new MyException(MyException.STUPID_ERROR)And then in a unit test, you can have:try { doSomethingWrong(); fail("Should have thrown MyException."); } catch (MyException me) { assertTrue( "Should have had key=" + expectedValue, me.getKey() == expectedValue); }There are then ways to make this fancier to handle things like line numbers, file names, and other metadata about an exception in a checkable way.

Read: Keyed Exceptions and Negative Test Cases

Topic: The Service Garden Previous Topic   Next Topic Topic: HOWTO Include ASL Licensed Software in GPL(ish) Software

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use