This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have written a DAO, which contains four methods (insert,update,retrieve and delete). Now I want to test them using JUnit.But I have never worked on JUnit. Can someone help me out by telling me on how to test those methods thru JUnit.
Most people write JUnit tests with few or no displays. When you run a bunch together it's clean to just let JUnit display dots as they run.
The assert statements that you commented out are key. At the end of the run JUnit reports how many passed or failed based on those asserts.
You can remove a ton of try-catch code and just let exceptions fly. Declare your test methods "throw Exception" and let JUnit report the exceptions.
If you expect an exception, you can do this:
Testing with a real database is a bit of a pain, but it is your whole point for this thing. You have to assure that the database is in a known state before you begin. You can use the setUp() method to delete all rows or certain rows before you start, and maybe use tearDown() to delete them again so you don't leave test stuff behind.
Any of that sound good?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Aug 27, 2003
Does JUnit generate any reports (HTML,Text) of the result. If yes, then how can i do it. I know it can be done thru ANT but i am not using it. Mine is a standalone class.
Joined: Jan 29, 2003
You can run JUnit as a text program and all the output goes to console, so you could redirect that to a file. You can also run a provided Swing front-end that has a neat green bar but I'm not sure about capturing the console output. And I haven't looked at the Ant task at all. It might be something you could modify to capture what you want. I hear about people putting the test results on web pages all the time, so it's gotta be doable. The Ant task sounds like the most promising place to look.