Slashdot Mirror


Pragmatic JUnit Testing

Will Gwaltney writes "The Pragmatic Programmers have done it again. This time it's with volume 2 of their Pragmatic Starter Kit series, Pragmatic Unit Testing in Java with JUnit. In the friendly, accessible style we've come to expect from Andy Hunt and Dave Thomas, the book provides a gentle introduction to unit testing in general and the JUnit testing package in particular. Of course, by the time you've read through the introduction you're hooked, at which point Andy and Dave give you enough information to make you not just a good unit tester, but a better developer to boot." Gwaltney offers this disclaimer: "I know both authors and was one of the proofreaders for this book. I have no financial stake in either the book or their company." With that grain of salt, read on for the rest. Pragmatic Unit Testing in Java with JUnit author Andrew Hunt and David Thomas pages 159 publisher The Pragmatic Bookshelf rating 10 reviewer Will Gwaltney ISBN 0974514012 summary Unit Testing introduction, tutorial, and developer's guide

What's the Approach? You can go a long way in the unit testing world with this book. It's a great starter guide for learning about unit testing; the first chapter has several small sections which answer many of the questions a novice might have about the subject, including some of the most common objections to unit testing. Then the book launches into a detailed user guide to the JUnit package, including some useful but lesser-known topics like writing your own custom asserts.

After you've got the basics down, the book spends several chapters drilling into the concepts, considerations, and best practices for writing good tests. This section of the book is quite practical, and is where the true worth of the book comes out. Any developer who reads this section will come away better for it even if they never consistently write unit tests. It's amazing how many testing issues turn out to be development issues at their core; this section of the book points some of them out. Simply being aware of these issues can help you to avoid a number of common pitfalls.

Finally, the book discusses common unit testing problems and their solutions. This section is a lot of fun because of the anecdotes that Andy and Dave include; unfortunately some of them I recognized from my own experiences as a developer. (What, me do that? No, uh, of course not!)

Who's it For? This book is primarily for developers (and testers who are intimately involved in the development process), since unit testing per se is primarily a developer task. Depending on the organization of the software shop, by the time code gets to a formal testing or QA group it's probably too late to apply unit testing methods effectively. (Note I said unit testing methods here.) The book is ideal for the unit testing novice, and there's a lot of good information here for more advanced developers to ponder.

The book is targeted at those who want to learn "how to think about tests, how testing affects design, and how to handle certain team-wide issues you may be having" (from the Preface). In particular the book is an excellent source of information for those developers who have recently moved into the Java area and are looking for a good testing framework.

No matter what your role in the development process, this book can help give you a fresh perspective into unit testing and related issues. It prods you to think about issues that perhaps you hadn't considered before.

Pros As you can probably tell, I like the Pragmatic Programmer writing style. It's informal and friendly without being chatty or wordy, and the main text moves along well. More technical discussions are reserved for sidebars, and there are "Joe Asks..." sections throughout the book that anticipate questions that Joe Programmer might ask about the current topic.

The examples in the book are excellent. Many books err on the side of code snippets that don't have enough context to be understandable, or multi-page code extravaganzas that bury the details of the topic at hand under mountains of cruft. I found the examples here to give just the right amount of detail to understand what's going on. And the source for all the examples is available from the author's web site.

Cons What this book is not is a reference manual for JUnit. The book doesn't include a javadoc listing for the JUnit package, nor does it have a section that digs into JUnit's internals. This is not necessarily a Bad Thing because that information is available from the JUnit web site, but it does bear mentioning.

If there was anything I found to complain about it was the use of acronyms in the concepts section of the book. I'll admit right here that I'm no lover of acronyms, so I felt that using things like Right-BICEP, CONNECT, and A-TRIP as mnemonics for the concepts was a bit over the top. (Read the book to see what I mean.) However, I have to admit that they help me remember the concepts discussed, so I guess they do their job (grumble).

Why Should I Read this Book?

If you hate testing, this book will very probably change your mind, and you'll have fun in the process (the stories are great!). And even if you still hate testing after you've finished the book, you will have enhanced your development skills enough to be able to delude yourself that testing isn't necessary for you (well, maybe not that much). At the very least, this book will make you aware of the various issues that proper testing can expose and maybe help you try to avoid them. By shining a light into these areas, it can still make you a more effective developer... maybe effective enough to realize how much more solid your work could be with this type of testing coverage.

If you love testing, this book will remind you why you began the love affair. Your love will be strengthened and deepened, and the tests you write will undoubtedly get better, too.

For Java developers specifically, this book will get you started writing JUnit tests painlessly and effectively and will take you a long way down the road to unit test proficiency. It will give developers in any language quite a bit to take "back to the terminal" regarding design and process as well as testing. An excellent and much needed book.

Like volume 1 of the Pragmatic Starter Kit series ( Slashdot Review), this book is available from the Pragmatic Programmer website. The printed version is $29.95 and there's a PDF version available for $20.00. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

118 comments

  1. I've read both books by Pingular · · Score: 2, Informative

    and I prefer this one. Just my opinion though.

    --

    When anger rises, think of the consequences.
    Confucius (551 BC - 479 BC)
  2. That Grain's A Boulder by tomblackwell · · Score: 4, Insightful

    It might be wiser to trust someone slightly more removed from the creators and creation of this book with respect to its utility.

    1. Re:That Grain's A Boulder by happyfrogcow · · Score: 4, Insightful

      i think you're blowing it a bit out of proportion. the review clearly stated pros/cons/intended audience and more.

      would you take a beta-testers review of an online game different? I bet you would be glad to have the behind the scenes insight as to whether or not the game sucks. If you were smart you would do just as was said and "take it with a grain of salt". continue to look for other reviews and file this one away in your head for later.

    2. Re:That Grain's A Boulder by Short+Circuit · · Score: 2, Funny

      At the end there, I began expecting to have children with my testing software. (I'm certainly not likely to have children any other way...)

  3. Pragmatic Extreme Programmer... by kompiluj · · Score: 5, Funny

    Jeez, I like being both pragmatic and extreme when I use JUnit.

    --
    You can defy gravity... for a short time
  4. Is this book good for C++ programmers, too? by plover · · Score: 4, Interesting

    I guess the question is, are the concepts in the book presented generically enough such that it would port (for a sufficiently loose definition of port) to another framework such as CppUnit?

    --
    John
    1. Re:Is this book good for C++ programmers, too? by mrob2002 · · Score: 5, Informative

      They certainly are. Most of the book is about how to write good unit tests, e.g. test the boundary conditions, and how to think clearly about what the boundary conditions are. I took enough away to start using nUnit successfully (nUnit is a framework for .Net unit testing). I've found unit testing extremely useful in day to day development, and this book taught me how. It's also available in PDF from the authors' web site if you prefer that format.

    2. Re:Is this book good for C++ programmers, too? by plover · · Score: 2, Informative
      Thank you, I'll have to add this book to my ever-expanding "In" queue of books. I've needed to "automate" my testing for some time now and while JUnit seems like the right way to go, it doesn't do much for a C++ project. But I've been reading up on CppUnit and I think that's the way I'll go from here.

      Thanks again.

      --
      John
    3. Re:Is this book good for C++ programmers, too? by (startx) · · Score: 3, Informative

      I'd also suggest looking at unit++ for your unit testing needs in any c++ project. Very easy to use, and just seems like the c++ way of doing things. Some of the "ports" of junit to c++ still seem to javaish for my liking.

    4. Re:Is this book good for C++ programmers, too? by caseydk · · Score: 4, Interesting



      Actually, I caught a presentation from Andy Hunt early last month and he talked about this directly.

      They're releasing a version for C# shortly and they have the intent of releasing the book for a few other languages.

      The most interesting part is that he told us that they're working to keep the language-specific content seperate from the main text. Therefore, they can write the applicable stuff in a given language and then LITERALLY run a makefile to make the next book.

      Pretty sick (yet, pretty sharp), huh?

  5. JUnit? by Lando+Griffin · · Score: 5, Funny
    Isn't that 50 Cent's group?

    "JUnit, yeah, we'll take your ass out. JUnit, yeah, that's what we're about!"

    1. Re:JUnit? by Anonymous Coward · · Score: 0

      Crew, man, it is crew...

    2. Re:JUnit? by the+Man+in+Black · · Score: 1

      What's sad is, I clicked the topic specifically to write

      "J-J-J-J-J-J-J J UNIT!"

      Wonderfully, I wasn't the only one with that thought.

    3. Re:JUnit? by hookedup · · Score: 1

      Sadly, I was going to do the same too...

    4. Re:JUnit? by Anonymous Coward · · Score: 1, Interesting

      No, Junit was a minor Egyptian goddess. So minor, in fact, that I can't find any specific information about her apart from this image (warning: contains nudity).

      Personally, I'll never use this thing because I used to work with somebody who was sure that it didn't matter if his code ever worked, or if it was documented or if it did what it was supposed to do, as long as he wrote enough tests for it. Of course, anybody who didn't agree with him was automatically anti-testing and thus responsible for the project failing (nothing to do, of course, with the fact this guy had no idea what he was doing). Argh.

      The company, you'll be glad to know, is out of business, so you'll never use that particular product, but the guy is alive and well corrupting code belonging to other companies.

      Sorry, went on a tangent there, but 3 years later I still need to vent.

  6. Wonderful book by grub · · Score: 1, Troll


    Pragmatic JUnit Testing was the quantum leap I needed. Because of Pragmatic JUnit Testing, the lateral shift to JUnit was perfect. It wasn't a complete shock thanks to Pragmatic JUnit Testing. Overall I'd say it wasn't so much a complete paradigm shift but thanks to Pragmatic JUnit Testing, it helped me think outside the box.

    Three cheers for Pragmatic JUnit Testing!!

    Hip hip.. HOORAY!

    --
    Trolling is a art,
    1. Re:Wonderful book by donnyspi · · Score: 1, Troll

      If I had mod points left, i'd give you -1 for using the phrase "paradigm shift". That is the most tired, overused phrase in the book.

    2. Re:Wonderful book by Anonymous Coward · · Score: 0

      The whole post was overused phrases, that was the point. :)

    3. Re:Wonderful book by PragDave · · Score: 1

      Except the word 'paradigm' doesn't occur in the book... :)

    4. Re:Wonderful book by Neil+Blender · · Score: 0, Offtopic

      I am assuming you are from the Pragmatic Bookshelf based on your username. You might be interested to know that prag is slang for a prison bitch.

    5. Re:Wonderful book by r.jimenezz · · Score: 0, Troll

      Searched the web for pragmatic junit testing. Results 1 - 10 of about 2,540. Search took 0.25 seconds 1. The Pragmatic Programmers, LLC Sorry dude, will have to repeat "Pragmatic JUnit Testing" more in your next post...

      --
      The revolution will not be televised.
    6. Re:Wonderful book by Anonymous Coward · · Score: 0

      You might be interested to know that prag is slang for a prison bitch.

      My condolenses for the flashbacks this has brought up.

    7. Re:Wonderful book by Anonymous Coward · · Score: 0
      Honey, you ain't had nuthin' 'till you've had a 325lb woman sit on your face. Your life truly flashes before your eyes as you pray she'll climax and roll off before you get brain damage.

      Everything else is just pullin' your pud.

  7. The first book in this series.... by tcopeland · · Score: 5, Informative

    ...was a good one for learning about CVS.

    Especially helpful was a small appendix which listed various recipes; i.e., the series of 3 or 4 steps necessary to merge a bugfix from one branch to another or whatever. I always forget the order of those cvs -j options...

  8. from the better-than-sex dept. by r.jimenezz · · Score: 1, Funny

    Sigh :( Such is the life of the geek...

    --
    The revolution will not be televised.
  9. extreme programming by tijnbraun · · Score: 1, Interesting

    Is Junit any way linked with extreme programming? It sound that their goals are quite identical. However their ore informationmore informationsection doesn't mention JUnit anywhere.

    1. Re:extreme programming by Abcd1234 · · Score: 4, Informative

      Err, JUnit and Extreme Programming are really pretty orthogonal. XP, which is a programming methodology, preaches the idea of writing unit tests before writing code. JUnit is a unit testing framework for Java. Therefore, JUnit can be useful tool in the practice of Extreme Programming, as it is designed for the express purpose of simplifying the creation of unit tests.

    2. Re:extreme programming by plover · · Score: 4, Informative
      Only in that extreme programming is a test-centric philosophy that requires you to continually recompile/test while developing.

      My understanding of JUnit is that it's a test framework that simplifies running all the tests you've developed in conjunction with a project. So, if you use the JUnit foundation to build your unit tests, theory says that before turning your code over to the QA teams you'd run JUnit over the whole ball of wax to make sure you aren't sending out something stupidly broken.

      But your question: "is JUnit linked with Extreme Programming" is a lot like asking if C++ is linked with Borland or Microsoft. If you're into extreme programming in Java and testing with JUnit, then yes, they're linked. And if you're into extreme programming in Java, then you're probably already using JUnit.

      --
      John
    3. Re:extreme programming by tijnbraun · · Score: 1

      Although I think that XP has some valuable ways of bugs to be found. My link should have been "more information".
      The ore of bad programming can be probably found elsewere.

    4. Re:extreme programming by Anonymous Coward · · Score: 0

      Let's see, from juinit.org....

      JUnit is a regression testing framework written by Erich Gamma and Kent Beck...

      From the credits for Extreme Programming Explained....

      Kent Beck (Author), Foreward by Erich Gamma

      Besides which, junit.org has links to 3 other extreme programming sites, so i'd say you're onto something...

    5. Re:extreme programming by Thuktun · · Score: 1

      Is Junit any way linked with extreme programming? It sound that their goals are quite identical. However their ore informationmore informationsection doesn't mention JUnit anywhere.

      Well, JUnit's website has the title, "JUnit, Testing Resources for Extreme Programming".

    6. Re:extreme programming by JollyRogerer · · Score: 0

      Umm... Ever heard of Kent Beck?

      --
      "Ever heard of spell-checking?"
  10. G-Unit. by bad+enema · · Score: 4, Funny

    But I applaud your attempt.

  11. Nope. Sex is still better. by bad+enema · · Score: 1

    I guess I'm shallow like that.

  12. Re:Java by tcopeland · · Score: 5, Informative
    I use a slew of JUnit tests for a project I work on (PMD) - you can see all the code for the tests here (thanks JXR!).

    Here's an example of one of the PMD JUnit tests that checks the command line option processing:
    public void testShortNames() {
    CommandLineOptions opt = new CommandLineOptions(new String[] {"file", "format", "ruleset", "-shortnames"});
    assertTrue(opt.shortNamesEnabled());
    }
    and another that makes sure that an Exception is thrown in another case:
    public void testNullArgs() {
    try {
    new CommandLineOptions(null);
    fail("Should have thrown an exception when null passed to constructor");
    } catch (RuntimeException re) {
    // cool
    }
    }
    When I make a code change, I just rerun all the tests to make sure I didn't break anything. If someone reports a bug, I add a test so that bug won't reappear. Sweet.
  13. Does it really matter? (n/t) by Anonymous Coward · · Score: 0


  14. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    So you're against extra documentation and over-verboseness?

  15. Re:Unit tests are a bad idea by Pfhreakaz0id · · Score: 0, Troll

    My thoughts exactly. Before that was my first thought "who the hell, exactly, has time for this in a real job?"

  16. Re:Unit tests are a bad idea by tcopeland · · Score: 4, Interesting

    I'm not sure if this is a troll or not... but, what the heck.

    > Extra code == extra bloat

    Writing tests helps student understand their code and track down bugs. If you don't want students to submit tests, tell them not to.

    > Too much documentation

    What does JavaDoc - which the compiler discards - have to do with unit tests?

    > Unit tests are written with no extra
    > skill than the code they test

    Don't throw the baby out with the bathwater. If a student is writing tests, at least he's trying to improve his code quality. You're a grad student; show him how to write better code _and_ better tests.

  17. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    You sir are an idiot. You obviously have never left the confines of your little academic cave and been out in the real world. How the hell you got into Harvard is a mystery to me and just validates my increasing suspicions that Ivy League schools are no better than the local community college.

  18. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 2, Informative

    Junit tests are meant to be fairly simple and if used correctly (e.g. run after every change) can really help to reduce bugs and thus save you time.

    where the whole unit testing idea falls down is when you have things you can't unit test like jsps and servlets, these require much more complex testing methods.

  19. Re:Unit tests are a bad idea by Xenographic · · Score: 5, Informative

    Odd, when I took an obligatory basic Java course, the grading was quite the opposite. They WANTED javadoc, they WANTED good comments, and they WANTED us to make sure the code worked right.

    They went so far as to give us a few test cases (and if we didn't pass those test cases, we got a zero).

    In fact, most of the grading process was to have their automatic system compare the output of our solution with the reference solution...

    That said, the TAs were known to be lazy and I don't think anyone ever actually read our comments. I found this out when my solution differed slightly from what they expected (you had to do something different than they expected to direct the output to a file, namely java SomeProg > output, which was WELL commented...) and they couldn't figure it out... (hell, they didn't understand what that DID when I talked with them...)

    So I can believe that you're a TA, but beyond that...

  20. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 2, Interesting

    You are a fool. Yes, it does add to the bloat of code that you have to read through (poor baby!) but considering the unit test isn't ever shipped as the final product it adds nothing to the finished product besides proof that the program might work.

    I cannot ever begin to express how amusing it is to hear a guy say that he wants professional coders and then complain that unit testing gives him more documentation to read. The horror!

    That reminds me, I need to call up M$ and tell them that I want a patched version of XP that doesn't include any help documents. Real coders don't need documents.

    If you have this TA, change your class now.

  21. Next week's book review by Anonymous Coward · · Score: 3, Funny

    Next week, Steve Ballmer reviews "The Road Ahead"

  22. Re:Unit tests are a bad idea by HarpMan · · Score: 5, Insightful

    Um, this is a joke, right? You teach at Harvard!!??? Now I know why we're losing all our jobs to offshoring. If this the best 'teaching' our educational system produce, we're really in trouble.

    Seriously, it sound like your objections are just based on laziness - you don't want to look at extra code, documentation, etc. Too much work for you. Oh, that's another reason we're losing to offshore folks - laziness.

    I guess we're illogical too. What does documentation have to do with unit testing? Can't you do one without the other???

    Sorry, I'm just flabbergasted.

    --
    Stephen Molitor steve_molitor@yahoo.com
  23. Re:Unit tests are a bad idea by Q+Who · · Score: 1

    Harvard CS department is not considered strong.

  24. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 2, Insightful

    I've tried my hardest to get the profs to teach i386 assembly and then C so that students understand how a computer works, but to no avail.

    Teaching i386 then assembly would be a sure-fire way to mentally scar and/or fail 99% of your students. You simply can't presume that all students have the level of competency in computing that learning i386 requires when they first start a computing degree.

    This competency can be picked up, and I'm not arguing that both assembly (to a certain degree) and a lower-level language like C are necessities for any good computing graduate, however a nicer ride at the beginning of the road is also a necessity. Giving someone an i386 reference manual and telling them to code won't produce more professional coders, it will instead drive away bright students who perhaps just don't have the necessary computing skills yet.

  25. Re:WORD by Anonymous Coward · · Score: 2, Funny

    my UID is lower than yours

    Apparently you beat me in the IQ department too.

  26. Re:Unit tests are a bad idea by outcast36 · · Score: 5, Insightful
    Your arguments are pretty valid for a TA who is trying to grade Java II homework assignemnts. But for people writing business systems there are some benefits.

    1. Extra code == extra bloat- In the business world we call this CYA. You write the tests, they run green. If something breaks, you didn't write crappy code.
    2. Too much documentation- Does anybody have this problem? I wish every system I touched had too much documentation
    3. Unit tests are written with no extra skill than the code they test- You bring up an intersting point here, and you are correct. For systems doing something novel or complicated, having the dev write the test makes no sense. However, most of the apps out here connect back to a CRM db, or fire off an email when something is done.... super boring stuff. A competent developer already has an understanding of these things and is more than capable of writing a test.


    anywho, your students are going to end up as PHBs & the rest of us would appreciate it if you didn't turn them off to testing before unleashing them onto the working world. Thanks.
  27. Re:Unit tests are a bad idea by TeaEarlGreyHot · · Score: 5, Insightful

    Every day I am reminded why the universities consistently fail to produce programmers who can work in the real world (assuming this wasn't just a troll).

    In the real world, code isn't handed in, marked, and that's that. Code is checked in, becomes part of a large, complicated system, and sometimes goes unvisited until years later, when entirely different personel have to take a gander at it and figure out what the heck it does, and why it was designed the way it was.

    The more comments and documentation, the better.

    In my current job, I'm in the middle of trying to untangle a badly-designed subsystem so it can be redesigned for new applications. There's no documentation, no unit tests. Only very high level descriptions of the I/O. The business logic is buried deep in the code, costing the company weeks of my time sifting through code. Some documentation on the outside would have cost a little bit more then, but saved a whole lot now.

    Furthermore, if your interfaces between subsystems are well-designed, JUnit should be all you need to warrant that YOUR code will interact with the rest of the project according to the specification. And if there's a problem, it's the specification's fault, not yours. Granted, JUnit isn't really used class-by-class, but it is very useful a step above that, when it comes to integrating your code into a large project.

    The problem in the academic world is there is no concept for how hugely huge projects get in the real world. No one is taught proper methodologies and good software engineering. Every assignment is a microcosm, and the short, quick, bang-out-the-code-and-ask-questions-later technique is what gets promoted in the process.

    I'm sure there are schools that aren't like this, and I'm making sweeping generalizations, but this is what I have seen.

  28. MOD PARENT UP by Anonymous Coward · · Score: 0

    See subject.

  29. I've read the book..... by dannyelfman · · Score: 0, Offtopic

    And no where in there does it talk any thing about Hamburgers or about adoption.

    Infact, the only thing I come to expect from Dave Thomas is that he's pushing up daises.....

  30. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 3, Insightful

    You know that instead of having to read all of this code you could just write a unit test yourself and determine pass/fail according to that. Then you wouldn't have to actually read (poor thing) the stuff that is given to him to read...

    Of course that would require the thought process of a real programmer. Seems the Ivy Leagues aren't all they were cracked up to be. I won't be hiring any Harvard graduates for quite some time...

  31. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    What do you expect from Harvard "No one ever gets a B" University?

  32. Of course not. by bad+enema · · Score: 1

    Does slashdot really matter? Does life really matter?

    It's all perspective.

  33. Wrong Dave Thomas by Anonymous Coward · · Score: 0

    It is the Dave Thomas from the old SCTV series.

    So take off, hoser ;)

  34. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    If the code needs documentation because it is doing something non-obvious, it is time to rethink the code

    This has always been my opinion about code with way too many comments. People seem to think comments fix the code, when they really are only explaining something illogical in detail. As for unit testing, people who promote unit testing alway add the clause "If used correctly...". Correctly How? In my opinion unit testing is an idea only suitable for very specific applications. To use it as a general rule is worthless.

  35. Re:Unit tests are a bad idea by RedMage · · Score: 5, Insightful

    Well, I teach at Harvard also... And I'm glad I haven't run into you! Seriously, such arrogance has no place in a teaching situation.

    1. Extra bloat my eye! What you're saying here is that you're a lazy bum and can't read through extra class files. If you want to do something constructive then how about instructing on how to lay out a project intelligantly?

    2. Too much documentation? Again you're just a lazy TA and have no place grading. Regardless of the merits of HTML as a documentation medium, documentation is usually much more useful in grading than the actual code: it shows the thought process behind the code. Sure, I'm interested in that the code works as advertised, but I'm also interested in how you got there. It makes my job much easier to keep my students from falling into common pitfalls.

    3. I do agree here, but not as far as the basic premise that unit testing is bad. Bad tests are useless, while a good test can be rather helpful, especially in a regression situation. Teach your students how to write good test and their code will be better. Punish them for writing bad tests and they'll never test.

    Obviously you're not too proud of your convictions, or you wouldn't be an AC. I feel sorry for any professors you work with. I know mine are happy to work with me...

    RM

    --
    }#q NO CARRIER
  36. Re:Java by Tumbarumba · · Score: 5, Insightful
    So what does a JUnit call look like

    JUnit unit = new JUnit.TestFramework.SimpleTest()
    unit.setExpectedResult(4)
    unit.setPassMessage('Okay, exponention works...')
    unit.setFailMessage('Uh oh...')
    NumberFormatter format = new NumberFormatter()
    format.setDecimalDigits(1)
    format.setLocaleFractionalSeperator('.')
    unit.setFormatter(format)
    unit.runTest(Math.pow(2))
    Nah, more like this:

    public void testPow() {
    assertEquals( "Unexpected Math.Pow result", Math.pow(2), 4 );
    }

    Seriously, kids, java is bad for you...
    Yeah, I can create a verbose, unreadable and unrealistic example in Perl, too. It doesn't make the language bad, rather it reflects badly on the programmer.
    --
    My business: Farstrider Studios.
  37. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 5, Insightful
    I'm posting anonymously for obvious reasons.

    You're totally clueless, and don't want anyone to know? ;-)

    I'm a teaching assistant (TA) at Harvard University, and I mark a lot of Java code as part of a job that supports my PhD studies. Code submitted to me with unit tests face an automatic 5 mark penalty, before i even begin marking. There are several reasons for this.

    You are a total moron...of course not many go to Hahvahd for CS studies. I wonder what your counterparts at real engineering schools like MIT or CalTech have to say about your viewpoint... Actually my best guess (and hope) is that you're a troll trying to land fish such as myself... ;-)

    Extra code == extra bloat. The unit test code is submitted as an extra class file, which means there is one more thing that I have to read over before I can judge their code on its merits.

    Oh, so the good programming practices of your students make your life more difficult - so you penalize them? You should be fired.

    I find that unit tests are a pain to compile, normally found deep in a directory structure where they make little sense.

    Of course the unit tests must be at the proper place in the directory heirarchy, or they wouldn't compile (Java namespace implementation). So, I doubt you know what you're talking about.

    Too much documentation. People who submit unit tests generally document the interfaces using javadoc, which just increases the amount of reading I have to do before I can assign a mark. HTML as a documentation format is stupid. There is nothing wrong with submitting code with comments at the start of a function or method and leaving it with that. If the code needs documentation because it is doing something non-obvious, it is time to rethink the code.

    Er, Javadocs aren't written in HTML...HTML is generated from Javadocs. As to your other remarks regarding code documentation, obviously you've never done any real-world programming. That is often a problem with PhD. candidates.

    Unit tests are written with no extra skill than the code they test. Unit tests induce a level of confidence in the programmer that their code works as a black box. This is a bad idea. Programmers should understand why each and everyone of their modules works the way it does, not assume that everything is well because it passes some rudimentary test that was created with the same skill level as the rest of their code.

    Code should work like a black box. That is the exact idea of encapsulation, a primary tenet of OO programming. You are quite obviously a troll with nothing to contribute to this discussion.

    As you can see, I'm against unit testing. I've tried my hardest to get the profs to teach i386 assembly and then C so that students understand how a computer works, but to no avail. If we are to regain our jobs from the Indians, we must be more professional coders.

    Ah yes, the final prong on the hook. How many of those Indian coders do you think do "i386 assembly" and "C"? Just around 0%...and don't fool yourself into thinking that either of those disciplines will do much to make entry-level coders better programmers. The important concepts are algorithmic, numeric and complexity oriented. Programming language isn't so important...and the more expressive and less bug-prone the better. Java is a pretty good choice, and has the added benefit of being widely used in the "real world" - a place you're obviously not too familiar with yourself.

  38. What if it's your job? by Anonymous Coward · · Score: 0

    But what about those of us who hate testing, and it's our job?

    1. Re:What if it's your job? by MrSelfDestruct · · Score: 1

      You get to live in the hell of your own making.

      --
      Some mornings it just doesn't seem worth it to gnaw through the leather straps. -- Emo Phillips
    2. Re:What if it's your job? by Anonymous Coward · · Score: 0

      No, you create the hell for the person that has to maintain your code 5 years from now...

  39. Re:Unit tests are a bad idea by Oligonicella · · Score: 2, Interesting

    Academia. Land of TA's who want to do as little work as possible to earn that money that supports their studies.
    Your 1,2,3 are all faulty if applied to the working world. Which, by the way, is where real life is.
    Your last sentence has a blob of irony in it. Can you spot it?

  40. Re:Unit tests are a bad idea by RedMage · · Score: 1

    Hey hey hey!! Watch where you're throwing that troll, dude! CS at Harvard is quite strong, our AC posting notwithstanding... We're not all jerks, ya know...

    RM

    --
    }#q NO CARRIER
  41. Re:Unit tests are a bad idea by zx75 · · Score: 1

    I know this'll be flamebait, but it is TAs like you that we (as a student myself) absolutely hate. You remove marks that we are working hard to earn for creating robust code that we go out of our way to prove that it works correctly, even in circumstances outside of the specification. 'It is less efficient' yeah right, am I taking a course on code optimization here? When we write code that is fast, efficient, but can fail on outside of scope or illogical input, we get reamed out because it failed. When we make code robust and only accept valid input, it gets thrown in our face as inefficient.

    Too often these days there is not enough testing done, resulting in buggy programs that are flaky at best, production code or not. You should be encouraging your students to do these things so that they can learn it the right way the first time.

    It may not matter to you that you'll write off 5 marks for handing in a complete program because it is 'bulky', but it matters a hell of a lot to the student that you just did that to.

    --
    This is not a sig.
  42. Re:Unit tests are a bad idea by perkr · · Score: 1

    You won't be hiring someone from Harvard U because of an anonymous shittalker on slashdot?? ;-)

  43. Re:Unit tests are a bad idea by wfberg · · Score: 1

    I'm posting anonymously for obvious reasons.

    I'm a teaching assistant (TA) at Harvard University, and I mark a lot of Java code as part of a job that supports my PhD studies. Code submitted to me with unit tests face an automatic 5 mark penalty, before i even begin marking.


    And.. you don't want the students to know this? Or you don't want the professor to know this? Isn't the marking supposed to be at least somewhat transparant? Why else post anonymously?

    Your points;

    1) extra code is extra bloat; I'm sure it's entirely up to you to disregard any testing code delivered that was not required. Although, shouldn't you be testing their code to begin with (rather than *only* reviewing it - full marks for style, but it doesn't compile)?

    2) too much documentation; well, yes, if every line is commented, that's too much documentation; javadoc on the other hand documents methods, not lines of codes; in fact, used correctly, it documents interfaces, not the implementation. Things like pre- and postconditions, race conditions, that sort of thing. Javadoc is great.

    And javadoc is not just HTML; and it's right there "at the start of a function or method". (Java only comes with methods btw, mr.Harvard).

    3) Unit tests are written with no extra skill than the code they test; this is usually true of any test; such is the nature of tests. Happily, it usually takes less effort to come up with tests than it does to come up with "perfect" code, which is why they're useful. For example; a parser should parse certain arbitrary documents in the specified grammar, and correctly at that, and not dump core. Coming up with the test-cases is easier than writing a parser.

    Your argument might just as well be applied to a whole slew of other IT methodologies that students have to learn, like for instance coming up with Use Cases or UML Class Diagrams; in the real world these are useful tools for communication with others, in school you end up implementing the stuff you designed yourself; so what?

    --
    SCO employee? Check out the bounty
  44. Re:Unit tests are a bad idea by Delirium+Tremens · · Score: 3, Insightful

    Come on, people, relax. The above comment (#8433108) is obviously a Troll.
    It is very well crafted, but it does show all the characteritics of a Troll. Honest tone, but inflamatory arguments. Personal testimony (supposedly), but anonymous post. 'Insightful' opinions, but lack of touch with the actual matter etc ...

  45. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    What?

    Don't you make important decisions based on troll comments too?

  46. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    In the business world we call this CYA. You write the tests, they run green. If something breaks, you didn't write crappy code.

    No. That means you have crappy code AND crappy testing. Unit testing is perhaps the least amount of testing you should do. It's so minimal, I think of it as nearly useless. Not entirely useless, but nearly. If you're not doing more comprehensive tests that take into account how components work together, then you are not testing at all. You can put together a dozen modules that all pass unit tests and have the full program fail disaterously. Unit testing proves nothing more than the module can pass the unit tests. It says nothing about the final quality of the code.

  47. Hey, I Found A Good Book! by tomblackwell · · Score: 4, Funny

    My friend wrote it! And I proofread it! And I'm submitting it to you! Strangely, I really liked it! Really!!

  48. Re:Unit tests are a bad idea by D-Cypell · · Score: 1

    This pretty much highlights where I find my faults in the system. Is it commented? Check! Is it Javadoced? Check! Does the output matched the expected output Check!

    Doesnt matter if the code is poorly designed, inefficient and doesnt use the most appropriate library classes.... it passed the checklist...A+

    Of course those that know me, know I have a fairly strong bias against formal education...

    but i can neither confirm not deny that! ;o)

  49. Re:Unit tests are a bad idea by ZX-3 · · Score: 1

    I've tried my hardest to get the profs to teach i386 assembly and then C so that students understand how a computer works, but to no avail.

    If you got a CSE degree at a lesser Ivy (Penn) in the early-to-mid 90s, you would have learned SPARC assembly in the 200-level compilers course and 68000 assembly in the 300-level digital design courses.

  50. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 4, Funny

    I'm posting anonymously for obvious reasons.

    I'm a teaching assistant (TA) at Beaver College, and I mark a lot of Java code as part of a job that supports my PhD studies. Code submitted to me that works faces an automatic 5 mark penalty, before i even begin marking. There are several reasons for this.

    1. Working code == extra effort. Code submitted that actually runs means that I have to read over before I can give the code an arbitrary, uninformed grade. I find that working code makes me have to figure out how to compile it, which is terrible since I have no clue what I'm doing.
    3. (I can't count.) Working code is written by people who are more skilled than I am. Working code induces a level of confidence in the programmer and they think they are better than me. This is a bad idea. Programmers should understand why each and everyone of their modules shouldn't work, and not assume that everything is well because it actually does what it was documented to do.

    As you can see, I'm against working code. I've tried my hardest to get the profs to teach abacuses and then flip lots of little switches so that students understand how hard it is to make a computer work, but to no avail. If we are to regain our jobs from the Indians, we must be more arrogant a-hole coders.

  51. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    What are you studying?

  52. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    Actually you can unit test things like jsp and servelets (read the book!) by using mock objects or in-container tests. Really simplifies the process.

  53. Re:Unit tests are a bad idea by plumby · · Score: 1

    The correct question is "who the hell can afford not to do this in a real job?"

    Our IT department (one of the largest credit card companies in the UK - so a "real job") adopted unit testing as part of the development process on everything from Java to Cobol about 2 years ago. Quality has increased significantly, and I don't think any of the developers would feel comfortable coding without it now.

  54. Question on the book by pcraven · · Score: 3, Interesting

    I've found it very difficult to write good tests on applications that were web-based, and highly dependent on the data. Does this book have good solutions for these problems?

    1. Re:Question on the book by congaflum · · Score: 5, Informative

      It does offer some solutions and advice to those, although I'd have liked to see more of the book devoted to it. I think most folk can easily see the benefits of unit testing when shown simple examples (like writing tests for a linked list or a temperature converter or something), but it takes a fair bit more effort to learn how to apply tests to "real world" code.

      From what I've learned so far, there are 2 main things that'll help you write unit tests for these sorts of situations: one is to mercilessly refactor your code as you go, and soon you'll probably find it naturally becomes easier to test. Seperate any database code from the application logic, for example, and you'll probably find that the application logic becomes pretty easy to test.

      The second thing is mock objects. The typical example of these is (again) database code. By making a little mock implementation that only *looks* like it's really writing to a database you can often then test other parts of your code without having the hassle of setting up sample data, cleaning it back out afterwards and so on. In web apps, you may find it handy to have a mock implementation that pulls page requests, effectively pretending to be the browser.

      One other thing, which sounds a bit smug but is actually very true: if you start out writing tests from the beginning of a project, it will probably remain easy to test. On the other hand, if you take an existing codebase and try to retrofit tests onto it, it's more than likely that you'll have to rearrange a lot of code in the process... Cheers.

    2. Re:Question on the book by cheezit · · Score: 2, Informative

      I asked this very same question a few years back in an Extreme Programming class. The answer we got to after discussion was something like:

      Refactor the design to handle the state of a transaction in a pluggable way; then create test transaction objects (that implement an abstracted transaction interface) and use those as part of the testing framework. That way the tested code is isolated from the state of the database.

      This really intrigued me as it turns the "UI at the top, db at the bottom" model inside out. I've never tried it on anything larger than a little app; it worked well there but I don't think it would scale to a complex transactional application, especially if transactions aggregate or show other dependencies.

      --
      Premature optimization is the root of all evil
    3. Re:Question on the book by sundling · · Score: 1

      Junit in action covers other unit testing frameworks that address what you're looking for including HttpUnit, Mock Objects, Cactus (which even does in container testing) and other related technologies. If you are trying to use J2EE and unit test, this is a fantastic book. The coauthor Ted Husted, is even one of the committers for the struts project.

  55. Re:Unit tests are a bad idea by dduck · · Score: 1
    Hmmm... The course I teach at the IT University of Copenhagen is not like that. In fact my area of responsibility is development tactics and strategies, including assertions, exceptions, logging, JUnit, Ant, Design by Contract, CVS etc.

    We learn from our mistakes and the world moves on... When I enrolled at comp sci at the U of Copenhagen there was no email, no web, no Java, no XP, no UNIX and no CSCW and HCI courses. That was a bad idea, so we learned.

  56. Re:Unit tests are a bad idea by Lewisham · · Score: 1

    Thank god for that. I was about to fire off a simple "Not here, buddy" comment, but I'm glad that someone who is actually a TA sorted it out.

    I'm studying at the University of Bristol right now, and we've been taught pretty well so far, I think. We started off with C (that guy wants assembler? Seriously?!) and then worked to Java, which helps a lot. Getting thrown in at the segmentation fault deep-end has it's advantages :)

    As soon as we moved to Java, it's been taught as part of the Software Engineering units, so it's put into the context of The Real World, rather than just: "This is the Java syntax, now write me some programs." We're all aware that the marking itself is done by unit testing (it's even stated on the submission page where we upload the work), and our lecturer has made us write programs where we used JUnit testing on it. If the program was great, you still only got 50%. The testing also had to prove something in order to start getting the next 50%. We were also encouraged from the outset to Javadoc as much as possible.

    So yeah, we've always been taught comments + testing == good, lazy == bad. I really couldn't believe that that was a rarity.

    Chris

  57. Why Unit testing is a GOOD idea by Anonymous Coward · · Score: 5, Insightful

    Responding to the post above, there are a number of 'hidden benifits' to writing Unit tests for Real Programmers (and not Teacher's Assistants).

    1) It's fun (sad but true). I write my test cases first, and that gives me a chance to challenge myself. I'll try and write tests that are very comprehensive or hard to pass (but only if they adhere to the specification). I then hide the tests while I implement the code to see how much I remembered.

    2) It helps me seperate the notion of System code from Client code. This is pretty important because I'm often both the system architect and the client coder, and it's often hard to keep the two distinct when you're doing both.

    By writing the tests first, you are forced to think of the (unwritten) code as a client of the system. You ask yourself, 'How should this module work? What are the most simple and logical interfaces?' You then write code to those interfaces, and end up with clean architecture. You then implement the code to pass the tests.

    While that doesn't sound revolutionary, I find that without tests I have a tendency to let the logic dictate the interface. For example, I find myself thinking, 'this method would be really efficient if rather than receiving an abstract widget, it took 4 int args' (or something like that).

    There are some other benefits to Unit testing (code stability, confidence, etc), but those two were surprising 'discoveries' when I started unit testing.

  58. Re:Unit tests are a bad idea by ameoba · · Score: 1

    You, like most grad students in CS departments (I'm one myself) are an arrogant, self-centered prick. You might as well be docking 5 points every time you see somebody use indentation that you don't approve of or tabs instead of spaces (or vice versa).

    You see, as the TA for the class, it's not your place to decide these things, it's the professor's. I'm sure that every time you do something arbitrary like this, the student is going to turn around and complain to the prof, who is more than likely to give them their 5 points back.

    --
    my sig's at the bottom of the page.
  59. Re:Java by Anonymous Coward · · Score: 0

    Yes... there are plenty of other things making Perl a bad language.

  60. Re:Unit tests are a bad idea by tbarrett · · Score: 1

    Most Unis have enough trouble trying to teach people how to write good code for 1k line projects. Big projects are well beyond their scope.

    Large-scale software engineering is just something you have to be involved in before you can really understand why the methodologies exist. Most graduates who get taught different approaches don't have the experience to see why they're needed.

    Companies need to understand this and take the time to train their fresh coders and make sure they're doing things right.

  61. There's another book occupying this niche by dsplat · · Score: 4, Interesting

    Kent Beck's book Test-Driven Development does a nice job of presenting where unit testing fits into Extreme Programming. It comes off as a bit of a cookbook and some of the examples are in Smalltalk, but even so I consider it a valuable addition to my bookshelf.

    --
    The net will not be what we demand, but what we make it. Build it well.
  62. (Advice) by Tsali · · Score: 2, Informative

    Don't worry you young students... the PHB's don't read your code, and your coworkers will hate you for writing it after you're gone.

    Do unit testing. Do simple coding. Do it for you. It's good for your soul. It'll save your career.

    Case in point - I was working on ASP pages that get user ID/password information from a db before validating to the next layer. I refactored the entry page so that I could test all the pieces parts since we have different clients who log in through the same page and get taken to other places. Anyhow, I typed in our demo account and password and got in - had about four tests... so then I tried four different variations on the password, camel case, and an extra space at the end thinking it would pass. Wrong. The new tests all bombed and let the person in. Why? Because SQL is set by default to be case-insensitive, and the code only checked to see if there was a matching username/password combination in the table. One line of code checking the actual password coming back fixed a bug we had in the system since inception. And who knows what else is in there.

    If it doesn't fit on a screen, then break it up or throw it away. It's not extreme - it's the only way to make sense of a large system. If you write code at that level, you only need Javadoc to tell why you wrote the story the way you did...

    You'll be much happier. Ignore the TA from Harvard and make yourself better. You don't need to be extreme to do so.

    J.

    --
    This space for rent.
  63. My junit tests just fine. by Anonymous Coward · · Score: 0

    At least, THATS WHAT SHE SAID!!!! (in Russian, no less!!!)

  64. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    It's not the professor's job either. Both the TA and Professor are paid to teach, not execute the civilian version of checking the shine on the recruits shoes every morning.

  65. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0

    ...and worst of all, it's completely believable that there are real people out there who think like that.

  66. Unit testing as quality control by nimblebrain · · Score: 4, Interesting

    Unit testing does not require eXtreme Programming. On the other hand, eXtreme programming does require unit testing. We've studied XP's practices, and they seem to be roughly broken into two groups: programming for ever-changing requirements, and quality assurances (refactoring, unit testing) to make sure the code doesn't fall apart.

    As many anti-XP pundits will point out, unit testing is worth applying to any project that you can apply it to, and there are precious few that can't.

    I do a lot of framework-level programming, and I must say that stability, as well as my confidence in the code, has increased substantially from putting unit testing into the mix. Some folks advocate TDD (Test-Driven Development) in the canonical sense (test first), but I find that the completely up-front test creation works in practice only when you have rock-solid easy-to-translate-into-source-code checklisted requirements. I compromise, and build the tests after the interfaces, rough algorithm and rough support objects have been put together.

    Here are a few things I've learned over my unit testing experiences:

    • If the individual test methods start to get beefy, spin off the pieces you can into English-sounding methods on their own, especially if it's something you find yourself using in multiple test methods. Methods for creating test objects and comparing the test results are usually prime candidates.
    • If you have a facility to use generics in your programming, use it for anything you've had to do exactly the same 3+ times over - you can cover off the generic with a single unit test module
    • Classes are easier to test if you make liberal use of interfaces or abstract base classes - this lets you create classes for use by the test that implement extremely rudimentary functionality (instead of asking a web service for a value, it returns "1", or gives back an error) and/or that can be used as counters/loggers for the number of times and with what parameters the services are requested.
    • Negative testing is important - if you don't explicitly test what out of bounds, null or otherwise invalid input parameters and states do to your object, you're leaving yourself open to a host of "undefined behaviors" which can cause bugs.
    • For unit testing lower-level classes, you'll often find that a unit-test-per-method works well. For unit testing higher-level classes, you'll find yourself keeping the class the same, but the test methods will vary the data that gets fed into it.

    I haven't figured out a good means to unit test concurrent code, but you can often stack the deck for fast-running processes by adding in delays in key spots to extend the exposure to weak spots.

    Unit testing isn't for everything. It coughs and sputters a bit when you don't have full control over the environment (databases, sockets, network configuration, user interaction). You can shoehorn it in, but it's usually better left for the likes of regression testing and functional integrated testing.

    For a quick start to functional testing, we've started test-driving TestComplete at our office; it's proven itself to be a capable tool (our lead QA man has put up a sign on the computer entitled, "I am Q-Tron; I Test Software While Mere Mortals Sleep" - it has been testing out communications while he sleeps :)

    --
    Binary geeks can count to 1,023 on their fingers :)
    1. Re:Unit testing as quality control by steve_l · · Score: 1

      Good summary.

      I'd point people at CruiseControl for rebuilding and running the tests every time CVS changes get checked in. That app sends us hate mail every time somebody breaks something, and it is fantastic!

  67. Re:Unit tests are a bad idea by Anonymous Coward · · Score: 0
    Java only comes with methods btw, mr.Harvard

    (Not the original AC, btw).

    Hint: A static method is not really a method.
  68. CppUnit by steve_l · · Score: 1

    I've used CppUnit in testing complex code, and the big problem in C++ land is that a catastrophic failure of one test (i.e. pointer trouble) can contaminate the process so badly the remaining tests fail. Java tends to catch up on such things faster, so contaminates the other tests less often. This makes it harder to write test suites that rigorously try bad things and which all run happily. But on the positive side, once you have done that you end up with a nicely robust C++ program, as you fix all the pointer defects it throws.

    Oh, the other problem is catching and reporting things like pointer errors; CppUnit is set up to only catch std::exception subclasses.

  69. Oops... by Anonymous Coward · · Score: 0

    ...the Pragmatic Programmers have farted again.

    Head for the hills, party people!