Upfront warning - I've been handed a C# post-development unit testing (NUnit) task. So far, most unit testing articles/resources have been for Java, but the concepts are pretty transferable. I have/will do searches to see if my questions have been covered before, but bear with me if I come back often. Also, I am very new to unit testing, mock objects, etc. - End warning. * Would I need to/how would I testing my Printer class in isolation if it threw my custom PrinterException? PrinterException just extends Exception. Since it is a simple class, would I include it when testing Printer/ create a stub or mock object for (using unfamiliar terminology here)/ remove its usage from Printer and not use it at all? ** I'm asking about a specific example here, but I'm mostly looking for advice on how to apply the 'test in isolation' rule. * Any ideas on how I would test my WebServicer class that calls a Java web service (which accesses a database)? * Any ideas on how I would test my Logger class which uses a C application to send status information over TCP/IP sockets? A very brief descripion of the code to be tested would be: It is a front end that calls Printer, Logger, and WebServicer classes. Printer errors are sent to a server via the Logger (via a C socket app). WebServicer calls a web service which accesses a database. I'm planning to start small with testing the Printer class. Any ideas on research, approaches, specifics would be welcome! Thanks.
* Would I need to/how would I testing my Printer class in isolation if it threw my custom PrinterException?
You can simply catch the PrinterException in your test code and 1) ignore it if you expected such an exception to be thrown for your particular input, or 2) fail the test with a message like "shouldn't have thrown PrinterException" (in JUnit, there's a method fail(String) which does this -- I'm sure NUnit has a similar method available for test classes)
* Any ideas on how I would test my WebServicer class that calls a Java web service (which accesses a database)?
How do you invoke the web service? Do you really need to test the "WebServicer" class (i.e. does it have functionality that can break)? In general, you should refactor your WebServicer class so that it doesn't handle all the networking stuff by himself -- if it uses an interface called "MyHttpClient" or "MyWebServiceStub", you can create a mock object of that interface and hand that to the WebServicer class.
* Any ideas on how I would test my Logger class which uses a C application to send status information over TCP/IP sockets?
Open a socket in your test class, call the logging method, and verify that your socket listener received the message correctly? I think you should also consider joining the nunit or nunit-users Yahoo! group -- they probably have way more NUnit knowledge that most JavaRanchers.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus