Prev: Basic JUnit questions
Next: Discount CHLOE True leather AAA Wholesale (www.supertradeonline06.com )
From: Rhino on 19 Mar 2010 15:55 Does anyone know of an online resource that suggests exactly how to do unit tests on various kinds of methods? A resource that was focussed particularly on Java would be especially useful. I don't suppose it matters if the site is focussed particularly on JUnit, although that it what I am using. I am mostly interested in best practices for how to test various kinds of methods and classes, particularly ones that may be a little unusual. It's relatively self-evident how to unit-test a getter or setter, for example, but not quite as self-evident how to test a constructor or a hashcode method. And in my case, I'd benefit from the advice of experts even on the types of tests that seem relatively obvious. The ideas that I learned umpteen years ago for how to test may have changed somewhat by now.... Basically, I'm looking for a website that would answer this kind of question: 1. Given a setter method, is it sufficient to just attempt to set it to one value, then that is within the range of its datatype, then use the corresponding getter to make sure the value was set correctly or should a proper test try to invoke the setter with values that are the wrong datatype or out of range for the variable (e.g. if the setter is for a short, suppy a value that is long or float to make sure that the right things happen). 2. Given a getter method, is it sufficient to simply invoke it once and see that it is the value that should be coming back? If not, how what more should you do? 3. How do you test a constructor? 4. How do you test a method that has a void return? I expect to have a bunch of other questions along these lines so, rather than hopping on to this newsgroup for each of them as they come along, I'd prefer to start with some principles. I'm hoping the experts here can point me to something that is well- regarded by the Java community. -- Rhino
From: markspace on 19 Mar 2010 16:41 Rhino wrote: > Does anyone know of an online resource that suggests exactly how to do > unit tests on various kinds of methods? A resource that was focussed > particularly on Java would be especially useful. No websites, sorry. There are several books on the subject, however. xUnit Test Patterns: Refactoring Test Code by Gerard Meszaros I *think* specifically talks about Java but also includes generic patterns for all types of languages. It might be worth looking at. Regarding "how to test," one way to do it is to "white box" the method (that is look inside to see how it works, as opposed to "black box" which means you don't look at the implementation). Look at each branch or if-statement in the code, and make sure your test exercises each branch, both for the true and the false case. That way you at least have "code coverage" for all code blocks, and no block goes untested. So for a simple constructor like this public MyObject( String value ) { this.value = value; } You can just call with any old string and you've tested it. Whereas with this: public MyOtherObject( int value ) { if( value < CONST1 || value > CONST2 ) { throw new IllegalArgumentException(); } this.value = value; } You want to make sure that you test three values, one on either side of the valid range and one within the range. There are two blocks of code, the bit that throws the exception, and the bit after that assigns the value. Make sure each of the blocks is exercised and runs correctly. Etc. Just keep looking for code branches and blocks, and try to exercise each one, and your test will at least be decent. There are other, more sophisticated tests, such as those test to ensure an API complies with a specification, but that's a *compliance test*, not a unit test. Unit tests should be pretty basic and just make sure that nothing truly bad is happening. Leave the compliance tests to higher level code. For a hashcode: public int hashcode() { int hash = value; hash = hash * 17 + othervalue; hash = hash * 17 + morevalues; return hash; } I don't see any branches there. Call it once and that's all you need, imo. Again, super sophisticated tests are not the point of unit testing. Stability is. You should add some tests for correctness eventually, but those don't have to be super sophisticated, imo. If you can at least prove you can run with out throwing errors, that's the first step. Get that done first.
From: John B. Matthews on 19 Mar 2010 20:30 In article <Xns9D4097E2D5143noofflinecontactplea(a)94.75.214.39>, Rhino <no.offline.contact.please(a)example.com> wrote: > Does anyone know of an online resource that suggests exactly how to do > unit tests on various kinds of methods? A resource that was focussed > particularly on Java would be especially useful. Don't overlook the Cookbook and FAQ: <http://junit.sourceforge.net/> I just never get tired of "Where should I put my test files?" -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
From: Arne Vajhøj on 19 Mar 2010 21:20 On 19-03-2010 15:55, Rhino wrote: > Does anyone know of an online resource that suggests exactly how to do > unit tests on various kinds of methods? A resource that was focussed > particularly on Java would be especially useful. > > I don't suppose it matters if the site is focussed particularly on JUnit, > although that it what I am using. > > I am mostly interested in best practices for how to test various kinds of > methods and classes, particularly ones that may be a little unusual. > > It's relatively self-evident how to unit-test a getter or setter, for > example, but not quite as self-evident how to test a constructor or a > hashcode method. > > And in my case, I'd benefit from the advice of experts even on the types > of tests that seem relatively obvious. The ideas that I learned umpteen > years ago for how to test may have changed somewhat by now.... > > Basically, I'm looking for a website that would answer this kind of > question: > 1. Given a setter method, is it sufficient to just attempt to set it to > one value, then that is within the range of its datatype, then use the > corresponding getter to make sure the value was set correctly or should a > proper test try to invoke the setter with values that are the wrong > datatype or out of range for the variable (e.g. if the setter is for a > short, suppy a value that is long or float to make sure that the right > things happen). > 2. Given a getter method, is it sufficient to simply invoke it once and > see that it is the value that should be coming back? If not, how what > more should you do? > 3. How do you test a constructor? > 4. How do you test a method that has a void return? > > I expect to have a bunch of other questions along these lines so, rather > than hopping on to this newsgroup for each of them as they come along, > I'd prefer to start with some principles. > > I'm hoping the experts here can point me to something that is well- > regarded by the Java community. Maybe you could find an open source package with a huge number of unit tests associated and look at what they have done. Arne
From: Tom Anderson on 20 Mar 2010 15:47 On Fri, 19 Mar 2010, Arne Vajh?j wrote: > On 19-03-2010 15:55, Rhino wrote: >> Does anyone know of an online resource that suggests exactly how to do >> unit tests on various kinds of methods? A resource that was focussed >> particularly on Java would be especially useful. > > Maybe you could find an open source package with a huge number of unit > tests associated and look at what they have done. JUnit presumably has tests of its own. Those might be worth a look. tom -- I do not think we will have to wait for long.
|
Pages: 1 Prev: Basic JUnit questions Next: Discount CHLOE True leather AAA Wholesale (www.supertradeonline06.com ) |