Probably a good starting point is to use existing resources like reading the materials on http://www.junit.org, particularly those under the getting started link. Eclipse has stuff to work with junit as well and can make it easy to create a test. Please do a little reading first when there is plenty of material readily available out there, people get fed up with spoon feeding you the answers.
I think its commonly stated that you need not bother testing simple getters and setters, code like:
public Locale getLocale() { return this.locale; } public void setLocale(Locale locale) { this.locale = locale; }That is so simple it is very unlikely to be wrong and tools like eclipse can automatically produce these for you. However if your getters and setters are more complicated, may be doing checks on the value to ensure its valid (eg. checking an int is within an accepted range) then you probably will want to test them to check they actually do what you want them to with the value.
Example: If we had a class to represent a circle, the radius may be something we want to set and get. It does not make sense for a circle to have a negative radius, so the setter should not except values less than 0. If the radius value is attempted to be set to a negative value the setter should throw a suitable exception. You will want to test this, make sure a positive value can be set and that a negative value leads to the correct exception being thrown. You will probably want to make use of the getter to find out that the value was stored correctly.
public void setRadius(double radius) { if (radius < 0.0) { throw new IllegalArgumentException("Radius cannot be a negative number"); } else { this.radius = radius; } }OK, that setter is still fairly simple and is easily checked manually, but it still contains rules and by writing the test you are clearly stating the rules which this method must obey.
Michael Whapples On 09/12/10 05:01, John J. Boyer wrote:
I'm going to download junit from www.junit.org and have a look. As a matter of fact, I would like to test the localization module. I have a class with the methods setLocale, getLocale and localValue. How would I set up tests to see if these methods return what they are supposed to? John On Wed, Dec 08, 2010 at 09:03:53PM +0000, Michael Whapples wrote:I would say, don't look at it from a time angle, the real benefits are tests for all code written (only useful if tests are good) and the quality of design as thinking about how you can check something is right before you have something to test means you must consider what you will write more carefully and in more detail. I don't quite know how the idea of having someone writing the tests and someone writing the product code would work in TDD, for the simple reason you must write the test before you write the code and then you look to refactor the code afterwards to try and make it simpler and this may involve refactoring the tests. I think writing tests and writing product code become one activity in TDD. One of the risks I think which would be faced by splitting test writing and product code writing would be that if those writing product code get ahead of those writing tests, either you have people sitting around twiddling thumbs or you have product code written before tests and so break from TDD and may be loose the tests from that code. Michael Whapples On 08/12/10 20:08, John J. Boyer wrote:Good points. I'm sending a blind copy of this correspondence to the brailleblaster list for those who aren't also on this one. Is anyone interested in concentrating on writing the tests? On Wed, Dec 08, 2010 at 09:39:14AM -0800, Chris von See wrote:Using TDD will lengthen the development timeline a bit since you'll be writing both product code and test code (plus you may be maintaining the tests from time to time if/when your functional specification changes) but it will significantly shorten the debugging process since you'll be able to more readily debug individual portions of the code and you'll catch and localize functional regressions much more quickly. If you can find people that can focus on writing the tests themselves while others write the actual code in parallel you may be able to capture back some of that time... Cheers Chris On Dec 7, 2010, at 9:45 PM, John J. Boyer wrote:I think some on this list may be familiar with this technique. The subject has recently come up on the brailleblaster@xxxxxxxxxxxxx list. Will this approach significantly shorten the time to getting a usable application? Thanks, John -- John J. Boyer; President, Chief Software Developer Abilitiessoft, Inc. http://www.abilitiessoft.com Madison, Wisconsin USA Developing software for people with disabilities For a description of the software, to download it and links to project pages go to http://www.abilitiessoft.comFor a description of the software, to download it and links to project pages go to http://www.abilitiessoft.com