Here's my take on the issue. I'll try to respond to your statements in order you presented them.
Even bugfixing is fun. Unit testing is not.
Here I have to flat-out disagree. While bug detection and debugging might be challenging, there's almost always some kind of pressure involved. Especially for a system already in production. Plus, you never can tell how much time you will actually need. You may get that sucker in a few minutes, but sometimes you'll be stuck for several days. Where's the fun in that?
Writing a unit test is not that bad, I
can get over it. But the quantity of
work involed in testing it right makes
me shiver. I fear that I will spend
much more time on testing than on
actual implementation.
I have found out that for even moderately complex systems, unit testing is an integral part of actual implementation. How can you know, with at least some degree on certainty, that your code works as intended without trying it out? Unit-testing is just about approaching that trying-out part systematically, one part by one, in isolation.
More importantly, what about you, or for that matter, one of your colleagues, having to modify and/or improve that code some time down the road. Will you be able to remember all the intricacies involved? How will you make sure you're not introducing new bugs while fixing the old ones or adding a new feature?
I don't know much about unit testing, but a short look into books
like XUnit patterns made me realize
that I'd need months or even years to
learn enough about it to do it right.
I'd like to spend those years learning
about real developement skills.
Like others said, testing is a crucial development skill. Also, what other development skills do have on mind? How would you verify you are using them properly?
Then again, every development technique takes time, first to learn, and than to master. It's the same with testing.
We have a whole bunch of code with nearly no tests. The code is
known to be buggy ans was never used
in production, so we absolutely need
tests. I think I could spend several
months just writing tests and some
more month to make them all pass
before I can write my own code on top
of it.
What's the alternative here? Spend a few months writing code without tests, and then just push it into production. When Q&A, supposing you have them, starts piling up bug reports, how would you go about fixing each and every one of them?
I'm coding something and realize Hey, I need a method Xyz(int abc) for
that. I write this method in 30
seconds. If I now write all the tests
needed for Xyz() then I spend 5
Minutes on writing tests. After that,
I will have forgotten why I needed
Xyz() in the first place. It just
kicks me out of the zone.
Quite frankly, this sounds like a lame excuse. First of all, you are most certainly allowed to write a larger block of code, use the Xyz() method or whatever, and then write the tests for Xyz(). But, you're forgetting that with that 5 minutes spent on tests, you are actually investing the time now, instead of spending it on debugging later.
Tests also serve as an example of using your code for other members of your team. Unlike javadocs, they have to be runnable, and of, course, will complain if you change any part of your API or program logic, which may seem like an extra job from your personal point of view, but, what about other people depending on that code?
Writing unit tests has linear complexity (e.g. x tests for each
class / each method), but integrations
tests seem to have quadratic
complexity, as I'd need x * n * n
tests for n interacting classes.
There is only so much you can do with integration tests. Don't become obsessed with 100% code coverage, and of course, you are not required to replace the Q&A department with your tests. Unit test should check the behavior on classes in isolation, and once you're certain about it, you don't have to test all of that in combination with some other class.
For instance, if you're implementing a new kind of data structure, you would write tests against hard-coded data, checking the corner cases especially. But, you would not be writing integration tests concerning that structure from every placed that it is being used from.
On the other hand, if there are that much distinct cases regarding integration of your classes, then it may very well be you're dealing with an inherently complex problem. But let me ask you this, would you rather be redeploying your application again and again, stepping one statement at a time in the debugger, or would you write code representing common cases and run those specific cases in matter of seconds? Without the UI, without the need to enter the data again and again? And with the added benefit of that testing code being available at any point in time later on.
I've been programming for more than 16
years now, mostly as a hobby. If my
future would be doomed to writing
tests most of the time, this would
make me want to change my profession.
Don't get me wrong, but maybe you don't have enough professional experience regarding the delivery of a software product. The truth is, I originally saw little point in testing, and it was only after numerous bouts with debugger that I thought that there must be a better way. Long start-up times of J2EE servers also "helped" in me accepting the unit-testing as a methodology, since it felt much better to think about the code for a few minutes more, and write the reflection of what code is supposed to do in form of a test, then wait for 2-3 minutes for server to redeploy on each bug fix, no matter how small.
My opinion is that if you want to turn software into profession, you have to invest time into unit-testing. It is not a silver bullet and learning how to write testable code, what to test etc. takes time and patience. But, so does every activity you intend to do properly, right?