| «« Access to Information | Java Unit Testing Exceptions with JUnit »» |
|
About
I'm Ryan Lowe, a Software Engineering graduate living in Ottawa, Canada. I like agile software development and Ruby on Rails.
I write this blog in Canadian English and don't use a spell checker. Typos happen.
Projects
» Full-time Ruby on Rails freelancer
» Full-time with Rails since May 2005 » Former committer for RadRails (now Aptana) » I also have a few Rails side-projects in development: 1. wheretogoinTO.com Toronto nightlife 2. Hey Heads Up! TODO list and sharing 3. Layered Genealogy family history research 4. foos for foosball scoring 5. fanconcert for music fans (on hold) Hiring Rails developers? I can telecommute by the hour from Ottawa, Canada »» Email: rails AT ryanlowe DOT ca
BulletBlog
Now hosted on Hey! Heads Up -- check it out!
Syndication
Pings
Recent
Derek Lowe's (Ryan's older brother) words at Ryan's funeral
blog@ryanlowe.ca no more Forging Email Headers: Good, Bad or Ugly? Sarcastic Dictionary (Part 1 of Many) Tags Hierarchies Twisting Rails is Risky Business Risky Business? My Take on Early Alphas Whoa, it's August 2007 Closing Comments A Postscript to "Growth at the grassroots" »» All Blog Posts
Linkage
del.icio.us/ryanlowe
technorati/ryanlowe.ca/blog Aurora Roy Jim Andrew Trasker Travis Kibbee Karen Dr. Unk Ayana Van Bloggers Joel Spolsky Robert Scoble Tim Bray Dave Winer Raymond Chen James Robertson Ruby/Rails Bloggers rubyonrails.org weblog David Heinemeier Hansson Dave Thomas James Duncan Davidson Mike Clark Jamis Buck Signal vs. Noise Tobias Luetke Amy Hoy: (24)slash7 Jeremy Voorhis Eclipse Bloggers Planet Eclipse EclipseZone Luis de la Rosa Eclipse Foundation Kim Horne Billy Biggs Ian Skerrett Mike Milinkovich Bjorn Freeman-Benson Denis Roy
Archives
|
Code Covering ParserConfigurationException
Java 1.4 has a new library that deals with XML built into it called JAXP. In order to parse an XML document, you need a Document object. You can get a Document object from a DocumentBuilder object and you get DocumentBuilder objects from a DocumentBuilderFactory object. Yeah, I'm not sure if I like that either but it's the way they do it. This is where my 1 single line of uncovered code is: when you use the newInstance() method of the DocumentBuilderFactory to create a new DocumentBuilder if a "builder cannot be created with the configuration requested" the newDocumentBuilder() method throws ParserConfigurationException. This exception is a checked exception and should be dealt with and not terminate the program. Here was my method. How could I throw that exception and cover the catch line? protected void setupBuilder() I'm going to turn it into an unchecked runtime exception by catching it and throwing IllegalArgumentException. In the context I'm using the factory it would be a programming error to misconfigure it and then try to use it to create a builder. Programming errors, like using a method improperly, usually throw unchecked runtime exceptions like IllegalArgumentException. You might be wondering why I don't just make this function throw ParserConfigurationException. Because then I'd have to explicitly use a try/catch whenever I used it. If I change the exception to a runtime exception, that is not the case. So here's what I came up with: protected void setupBuilder(DocumentBuilderFactory factory) The key is to give the method a parameter that will change it's behaviour or output. Without an input parameter you cannot control the output and have good unit testing. Here are the test methods I wrote to completely cover the method: public void testSetupBuilder_Null() try fail("Expected NullPointerException"); public void testSetupBuilder_DefaultFactory() DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); public void testSetupBuilder_InvalidFactory() DocumentBuilderFactory factory = new DocumentBuilderFactory() public void setAttribute(String name, Object value) throws IllegalArgumentException } public Object getAttribute(String name) throws IllegalArgumentException try fail("Expected IllegalArgumentException"); The last test just implements a custom factory that throws ParserConfigurationException when newDocumentBuilder() is called. It's a pretty simple way to cover the catch block. So now I have 100% code coverage for AudioMan. Sweeeet. Posted at February 13, 2004 at 03:52 AM ESTLast updated February 13, 2004 at 03:52 AM EST Comments
You should have: public void testSetupBuilder_Null() try
public void testSetupBuilder_Null() try fail("Expected NullPointerException"); This is because you want to catch inside the try block so if it's not thrown then you'll know. Same for the It doesn't fail if the exception is thrown and then caught. The return; call stops the function before it can get to fail(""). This is a pretty standard way to test exceptions and has been documented on http://www.junit.org. » Posted by: Ryan at February 13, 2004 01:21 PMI didn't see that return. Good job. Will try not to post stupid comments again. » Posted by: Aleks at February 13, 2004 01:39 PMIt's OK, you inspired my next post. :) » Posted by: Ryan at February 13, 2004 02:10 PM |