Discussions

News: TSS Article: Testing Java Classes with JUnit

  1. TSS Article: Testing Java Classes with JUnit (14 messages)

    In this second part of the "TDD series" Wellie Chao looks at the advantages of JUnit as a testing framework, overviews essential testing concepts and terminology, and walks you through writing a sample JUnit test case. Upcoming articles include: Unit Testing of EJB components, Black Box and In-Container Testing of Servlets and JSPs, and In-Container Testing of Struts Web Applications.

    Read Test-Driven Development Series Part II - Testing Java Classes with JUnit

    Threaded Messages (14)

  2. This is not TDD either[ Go to top ]

    Just like the previous article in TSS' "TDD Series", this article doesn't touch upon TDD at all.

    The people who chose the name "TDD Series" for this article series seem surprisingly clueless about what TDD is about. (I don't know if it is the article author or the TSS editors, but it gotta be someone).

    Do yourself a favour: Read up on TDD or remove "TDD" from the article series name.

    Aslak Hellesøy
  3. Real world examples[ Go to top ]

    Some more useful examples to the general programming community would be good. There's probably not millions of people out there who care about calculating prime numbers or whatever. What about some example tests for database access code or some such?
  4. A starting point for a JDBC example:

    http://www.mockobjects.com/wiki/DevelopingJdbcApplicationsTestFirst
  5. Quality of Test Classes[ Go to top ]

    The featured test case - CalculatorTest - does the job to an extent, but does not support iterative development. To be fair, this article does allude to a notion of TDD by showing how to create a broken test case and then fix it (although a TDD process would usually begin with a test case rather than an existing application class!)

    However, a test class like CalculatorTest makes it difficult to iterate between test and app code, because the test is difficult to maintain - it contains a large amount of cut-and-pasted code and is in need of some serious refactoring.
  6. Agreed on examples[ Go to top ]

    Every time I read some article about using JUnit, the code being tested is always some bullsh*t math thing that NO ONE ever does. Then, when I try to use JUnit to test some real stuff, it ends up being a monumental mess of unmaintainable code, completely coupled to the code being tested, and an utter waste of time compared to just testing manually. Now, while most Java Process Zealots are pretty out of touch with the real world, I have to assume that someone who's actually smart with real-world experience thinks JUnit is useful and saves time. So, why can't we get an example of that? And that example should includ what happens when you need to make changes to the code under test.
  7. Agreed on examples[ Go to top ]

    I have outlined some counterarguments about the common "TDD takes more time" argument here: RE: Enough with the Test Driven Development Hype.

    If you think the process zealots are advocating that unit testing tools (like JUnit) should be used to ___test___ existing code, you have misunderstood completely. They are saying that these tools should be used in order to ____develop____ code that doesn't yet exist. What do you think the D's in TDD stand for?

    Also see: TDD is like teenage sex.

    HTH,
    Aslak Hellesoy - Any Language Process Zealot.
  8. Agreed on examples[ Go to top ]

    The classic argument to any nay-saying of TDD: You just don't understand it.

    Your "counterarguments" basically amount to just disagreeing with the various points you outline.

    The fact is that sometimes one must implement hugely complicated business logic and there is no way to simply test that. The project I'm working on now has such a complicated business process that test DATA to get even remotely decent coverage was huge. There's something like 40 or 50 unique paths through the code.

    So, a JUnit test case to test that is gonna be HUGE and it's gonna be complex, and in the end. For JUnit or Manual testing, I still have to come up with test data. Once I've got that, it's 10000 times easier to just run the tests myself. If I had written JUNit tests beforehand, I would've wasted huge amounts of time and not bought myself much of anything.

    And, hey, you are free to provide a real-world example; yet I never see one.
  9. Agreed on examples[ Go to top ]

    So, a JUnit test case to test that is gonna be HUGE and it's gonna be complex, and in the end. For JUnit or Manual testing, I still have to come up with test data. Once I've got that, it's 10000 times easier to just run the tests myself.


    I also find that with JUnit I can catch trivial errors only (I am talking about end-user applications, not generic libraries or db frameworks). Maybe it's important in the environment where develpers change often and don't have time to understand how the application works. However, the most difficult to find and costly bugs are in the application's business logic. Yes, I can write tests to test business logic as well, but before I do that I must come up with such data that will fail the test. The moment I come up with such data, I simply correct the code to deal with such data correctly: in this case writing JUnit tests becomes a mere substitution for developer documentation and this substitute is hard to maintain.

    And I'm too wondering why the TDD advocates don't provide examples to support their strongest points. All samples I've seen are simplistic test cases that do not apply to real situations.

    The problem here, I think, is that TDD is not equally applicable to all software being developed. There are categories of software where using TDD don't bring much benefits.
  10. Speed and quality[ Go to top ]

    "So, a JUnit test case to test that is gonna be HUGE and it's gonna be complex, and in the end. For JUnit or Manual testing, I still have to come up with test data. Once I've got that, it's 10000 times easier to just run the tests myself. If I had written JUNit tests beforehand, I would've wasted huge amounts of time and not bought myself much of anything."

    The benefit of a JUnit suite of tests is not to save time during the initial development cycle but rather to save time and ensure consistent quality in all subsequent development cycles by being able to run an automated suite of tests.

    Yes there is a maintenance overhead, and yes the initial task of writing them can be significant in any reasonable system, these need to be factored in to development schedules.

    Cheers,
    Martin
  11. Speed of development[ Go to top ]

    Agree in general but sometimes TDD approach forces better understanding of how code should work. It may increase a speed of development at initial stages, especially when disign is forming. For instance you can take a look at this sample of Test Driven Development. Unit tests rise attention and improve code quality.
  12. Agreed on examples[ Go to top ]

    I've been using JUnit in real world projects for few years now. In fact I'm writing this while my JUnit test suite is running (it takes about 5 minutes). If you want to see some real tests take a look at an open source library I maintain called Abra (it's another Java/DB persistence layer with code generations). We have handful of JUnit tests to test new releases. See http://http://abra.sourceforge.net/.

    My current at work project has a suite of about 60 tests and growing.
  13. Your example is smart,but can you talk about how to test db operation or IO operation with JUnit,thx.
  14. TDD Reflections from the UnitSide[ Go to top ]

    I think this article exposes some interesting issues that I thought were worth discussing. First, adopting TDD is not trivial as is shown by the comment about this not really being a TDD posting. Second, even a good set of unit tests can miss serious bugs. The test cases provided do not fully exercise the code and fail to expose and infinite loop in the factor() method. I posted some information on this on developertesting.com in a a blog entitled TDD Reflections from the UnitSide
  15. Alternative tutorial[ Go to top ]

    Ok, it seems averyone has been quick to jump on the relevance of this article, as compared to "true" TDD. Would anyone like to back it up by pointing us in the direction of an article that articulates just what the TDD philosophy is (apparently) about? Can there not be degrees of TDD? Just as there are degrees of XP?