Topic
7 replies Latest Post - ‏2007-02-12T01:26:07Z by zhengzwang
zhengzwang
zhengzwang
4 Posts
ACCEPTED ANSWER

Pinned topic How many unit tests will you write for a business method

‏2007-01-13T11:09:45Z |
Unit test is good in many aspects. But do you think it much expensive to write full set of unit tests for our business methods, especially enterprise application business methods?
The full set of unit tests include one or more ok test (a mediation business method require multiple ok unit tests), multiple data validation tests, multiple exception tests. Further, when you write in-container unit test, which is the most often seen condition for j2ee application development, you may experience pain on DbUnit, ServletTestCase or StrutsTestCase framework usage.
After nearly half a year practicing on unit test and test-driven development, I am frustrated very much, because I spent much more time on writing unit test code than on writing the business method implementation.
Can anyone tell me whether I should give up the unit testing?
Updated on 2007-02-12T01:26:07Z at 2007-02-12T01:26:07Z by zhengzwang
  • SystemAdmin
    SystemAdmin
    210 Posts
    ACCEPTED ANSWER

    Re: How many unit tests will you write for a business method

    ‏2007-01-13T23:54:40Z  in response to zhengzwang
    Quality isn't free per say (check out http://www.testearly.com/2006/11/07/quality-isnt-free/). It will cost you more to actively implement quality-- that cost can be time, effort, you name it. The pay off is longer term. In the last 6 months of hard work, have you seen any pay off yet? Think in terms of fewer bugs or more confidence to refactor here (see http://thediscoblog.com/2006/11/17/three-most-important-things-that-improve-code-quality/).

    There is no doubt that testing J2EE applications can be challenging. One thing to think about: if you find that the test is extremely difficult to code, it may be a sign that the code itself is complex. Even in distributed applications that can be tested with DbUnit and StrutsTestCase, there are aspects of the system that can be verified with true unit tests (check out http://www.ibm.com/developerworks/java/library/j-cq10316/).

    I'd recommend you not give up on testing-- make sure the business logic you are coding is easily tested. If it isn't, with 6 months of tests already coded, you should be able to refactor some of it to be easier to test!
    • zhengzwang
      zhengzwang
      4 Posts
      ACCEPTED ANSWER

      Re: How many unit tests will you write for a business method

      ‏2007-01-14T14:11:15Z  in response to SystemAdmin

      Thank you very much, AndyGlover.

      Really, what you say is truth, quality requires much effort.

      In fact, my frustrations these days came from the data validation unit tests. I have a business method with three String arguments. All these arguments are required to be not null, not blank and consisting of only letters. After writing 4 unit tests for this business method, I began to write data validation unit tests for it. However, I found it too tedious. 9 tests are required: testArg1Null, testArg1Blank, testArg1WithNonLetter, testArg2Null, testArg2Blank, testArg2WithNonLetter, testArg3Null, testArg3Blank, testArg3WithNonLetter. And I think that 'testXXXWithNonLetter' is difficult to write precisely, because there are many kind of characters that can be 'non-letter'. I can only write the 'testXXXWithNonLetter' using one kind: digit.

      Can you shed some light on how to reduce the number of unit tests, especially of the data validation ones, for a business method?
      • SystemAdmin
        SystemAdmin
        210 Posts
        ACCEPTED ANSWER

        Re: How many unit tests will you write for a business method

        ‏2007-01-18T03:44:49Z  in response to zhengzwang
        It sounds like one technique that could help you in this situation is to push that validation logic into a testable method like validateNotBlank, for example-- then you can isolate that method's functionality with a few test cases and not have to repeat this process for every method which then calls these validation methods. You could then create another validation method like validateOnlyCharacters (which could be solved efficiently with a regular expression).

        • zhengzwang
          zhengzwang
          4 Posts
          ACCEPTED ANSWER

          Re: How many unit tests will you write for a business method

          ‏2007-01-18T06:10:26Z  in response to SystemAdmin

          Sorry, I'm afraid I do not catch your meaning.

          I think data validation is part of business logic, so testing input data against the business method is reasonable. The data validation test does not need to know what implementation manner is used in the business method to validate the input, it only need to verify that certain input argument combinations will cause the business method to throw IllegalArgumentException.

          I my case, I have a business method with three String arguments, each with three validation requirements (not-null, not-blank, lettersOnly). And I use the Apache Commons Validator framework, which is a well tested data validation framework. Then how would you write the data validation unit test against the business method?
          • SystemAdmin
            SystemAdmin
            210 Posts
            ACCEPTED ANSWER

            Re: How many unit tests will you write for a business method

            ‏2007-01-24T02:51:07Z  in response to zhengzwang
            Commons validator is a good framework-- what I was implying is that if you have a generic validation method you can test that alone outside of your business methods-- then you can test your business methods for business logic and not so much for validation logic (as they'll call your already tested validation methods).

            If your business method takes 3 parameters each requiring three checks you don't need to write 9 tests-- I'd write a few tests for the validation methods and then write as many tests as I needed to verify the business logic portion of the business methods.
  • SystemAdmin
    SystemAdmin
    210 Posts
    ACCEPTED ANSWER

    Re: How many unit tests will you write for a business method

    ‏2007-02-10T19:14:49Z  in response to zhengzwang
    You may want to check out the article "Defensive programming with AOP" at http://www-128.ibm.com/developerworks/java/library/j-cq01307/ for some techniques in avoiding repetitive validation checks.
    • zhengzwang
      zhengzwang
      4 Posts
      ACCEPTED ANSWER

      Re: How many unit tests will you write for a business method

      ‏2007-02-12T01:26:07Z  in response to SystemAdmin
      Thank you, Glover.
      The content of that article seems showing a simple way of guard a method against invalid data, yet data validation unit tests are to guard 'the method is unbreakable by invalid data', and code refactoring will not inccur lost of those checks.