9 Replies Latest reply: Nov 10, 2010 9:39 AM by 811778 RSS

    JUnit

    csckid
      JUnit is popular tool for unit testing. But I haven't seen any example that proved JUnit is really important.
      Can someone show a specific example which shows real use of JUnit?
        • 1. Re: JUnit
          splungebob
          http://en.wikipedia.org/wiki/Test-driven_development#Benefits
          • 2. Re: JUnit
            796440
            csckid wrote:
            Can someone show a specific example which shows real use of JUnit?
            Every Java project at every company I've worked at for the last 8-10 years has used JUnit extensively, and benefited from it. That's a couple dozen examples of real use right there.
            • 3. Re: JUnit
              jschellSomeoneStoleMyAlias
              csckid wrote:
              JUnit is popular tool for unit testing. But I haven't seen any example that proved JUnit is really important.
              Can someone show a specific example which shows real use of JUnit?
              What are you asking about specifically?

              Are you asking if testing, any testing at all, is important/worthwhile?
              Are you asking if unit testing is important/worthwhile?
              Are you asking how junit is used with the above to make it important/worthwhile?

              If you don't understand the first two then the last makes no difference.

              And I would suppose that junit uses itself as far as real example goes.
              • 4. Re: JUnit
                csckid
                jschell wrote:
                csckid wrote:
                JUnit is popular tool for unit testing. But I haven't seen any example that proved JUnit is really important.
                Can someone show a specific example which shows real use of JUnit?
                What are you asking about specifically?

                Are you asking if testing, any testing at all, is important/worthwhile?
                Are you asking if unit testing is important/worthwhile?
                Are you asking how junit is used with the above to make it important/worthwhile?

                If you don't understand the first two then the last makes no difference.

                And I would suppose that junit uses itself as far as real example goes.
                I am really confused with
                Are you asking how junit is used with the above to make it important/worthwhile?
                I found out that JUnit is really important from jobs requirement advertisement. However, when I was doing few projects I didn't feel including JUnit code is worthwhile. So, if you could explain with right example then I would understand its real value.

                Lets consider an example from internet.

                import junit.framework.*;

                public class TestMath extends TestCase {

                public void testAdd() {
                int num1 = 3;
                int num2 = 2;
                int total = 5;
                int sum = 0;
                sum = Math.add(num1, num2);
                assertEquals(sum, total);
                }
                }

                Obviously sum of the result will be 5. Whats the point of testing this? More specific question, Why JUnit?? This test could be done with normal if-else statement.
                • 5. Re: JUnit
                  800330
                  JUnit is no magic, just a bunch of code that allows you to do unit testing without spending a lot of time on boiler plate code to make it happen. (if this helps you, I personally do think it's magic, fantastic and still have to grasp why it was deemed flawed and TestNG came in).

                  The example you chose may look simple, but is a very valid test if you are the author of Math.add(). It tests whether you did your work correctly in the add-method.

                  You can code the if-the-else for every case, but what about the reporting of failures. the asserts are short hands for consistent reporting and expressing the expected behaviour.
                  • 6. Re: JUnit
                    gimbal2
                    csckid wrote:
                    JUnit is popular tool for unit testing. But I haven't seen any example that proved JUnit is really important.
                    It is not, I use only TestNG. I don't touch JUnit at all.

                    Unit testing is really important, if you have business logic that would benefit from the existence of unit tests. For example, I create unit tests to make it easier to detect regression bugs. I create unit tests to do initial development tests of specific pieces of code before I put the code to work in a real environment ("test driven development"). I make unit tests to assist in testing the handling of fault paths in the code (something often overseen during real system tests).

                    But that is just me: I put unit tests to work for me. It really doesn't work the other way around, that unit tests automagically make things happen. It is not a service, it is a way of working (IMO).


                    I'll give an example case of where I make simple yet effective use of a unit test.

                    Initial: I get a bug report. Button 'X' is not working. I trace the bug to a certain piece of business logic.

                    Step 1: I create a unit test that tests the expected outcome. This unit test will at this point fail.
                    Step 2: I fix the code.
                    Step 3: I run the unit test again. If everything is correct, the unit test now passes.

                    I have the unit tests hooked into the release management system (Maven, through the surefire plugin). Until I fix the code, the unit test will fail and the release management logic will not allow me to create a new release version of the application. This makes it difficult (not impossible) to put known broken code into production.
                    • 7. Re: JUnit
                      796440
                      csckid wrote:
                      Lets consider an example from internet.

                      import junit.framework.*;

                      public class TestMath extends TestCase {

                      public void testAdd() {
                      int num1 = 3;
                      int num2 = 2;
                      int total = 5;
                      int sum = 0;
                      sum = Math.add(num1, num2);
                      assertEquals(sum, total);
                      }
                      }

                      Obviously sum of the result will be 5. Whats the point of testing this? More specific question, Why JUnit?? This test could be done with normal if-else statement.
                      No, it's not obvious. It's supposed to be 5, but if your add() method has a bug, it might be something else. This is why we test our code, rather than just assuming it works. Or do you always code 100% bug free from the start?

                      1. When you write code, you need to test it. Do you agree with this, or do you question it?

                      2. Your test code should be separate from the code it's testing. This keeps the real code free of clutter. You write the real code to do its job, and then you write tests that exercise that code, and make sure it produces the correct results.

                      3. As already stated, JUnit provides the infrastructure to support writing tests. It gives you methods to call that compare the expected output to the actual, mark the failures, and output the results in a readable format. You don't have to use JUnit. You could simply write your own test code with if (actual != expected) and so on, but you'll find yourself repeating a lot of code. At that point, if you're not an epic fail of a developer, you'll decide to put that repeated code into methods and classes. Duh. Once you've done that, you'll find you've basically reinvented the wheel and written JUnit. So why not use the one where the work is already done for you?

                      4. JUnit is one particular tool--a very widely used one--for testing small pieces of code independently. I'm sure there are other tools. Pick the one that suits you the best. When you apply for a job, if they say they want you to have used JUnit, more likely that want you to have used some unit testing tool. If you've used some other one, you can probably learn JUnit very quickly.
                      • 8. Re: JUnit
                        YoungWinston
                        csckid wrote:
                        JUnit is popular tool for unit testing. But I haven't seen any example that proved JUnit is really important.
                        Can someone show a specific example which shows real use of JUnit?
                        A specific one probably wouldn't be particularly useful, but consider this:

                        You're building a project.
                        Do you want to keep rewriting the same tests again and again? Or would you rather have a library of tests that you can simply re-use.

                        You want to add a new module to it.
                        Inline testing may be fine for path-type logic, but what about testing how the new module affects what's already there?
                        How about testing the interaction of modules with each other?

                        Testing may seem boring, but if you can think past the drudge it can be a fascinating part of programming. It can also help you write and document your programs better. My suggestion would be to get a book on the subject, because there's a lot to know. You might also want to get some literature on Extreme Programming and Test-driven Development.

                        And finally: JUnit is by no means the only test suite out there; just probably the most widely used.

                        Winston
                        • 9. Re: JUnit
                          811778
                          You'll fully appreciate the value of unit testing when you have a production issue that takes 24 frantic hours to solve, no sleep, too much coffee, with the business screaming at you because they're losing money, the girlfriend annoyed at you, and the office cleaner wondering why you're trying to bust a hole in the roof with a stress ball at 3am.

                          All because some doofus forgot to make a simple unit test for a seemingly easy-to-code method/requirement/class.