Win a copy of Spark in Action this week in the Open Source Projects forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

unit testing approach?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

* 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to take a look at http://xprogramming.com/xpmag/acsIndex.htm - it contains some articles about unit testing C# applications.
    Bookmark Topic Watch Topic
  • New Topic