7 Replies Latest reply on Jan 10, 2008 3:41 PM by 796254

    I can't understand JUnit technology...

    807601
      I can't understand JUnit technology...
      Please help me whith good documentation and one simple example.
        • 2. Re: I can't understand JUnit technology...
          807601
          dotn crosspost http://forum.java.sun.com/thread.jspa?threadID=5252860&tstart=0
          • 3. Re: I can't understand JUnit technology...
            807601
            One simple example, to test a calculator
            public class TestCalculator extends org.junit.TestCase {
            
               public void testAdding() {
                   Calculator calculator = new Calculator();
                   int a = 42; 
                   int b = 10;
                   int expectedResult = 42 + 10;
                   assertEquals(expectedResult, calculator.add(a, b));
                 }
            
            }
            Horribly contrived example, but basically, the unit test ensures that the calculator.add() method actually does what it's supposed to. I run my suite of tests, and Junit reports which ones pass and which ones fail. I haven't mentioned test-driven development, which is what JUnit is really handy for, but read the following book, it's the best TDD book I've come across

            http://www.amazon.co.uk/Test-Driven-Development-Practical-Guide/dp/0131016490/ref=pd_bbs_sr_2?ie=UTF8&s=books&qid=1199963095&sr=8-2
            • 4. Re: I can't understand JUnit technology...
              791266
              georgemc wrote:
              One simple example, to test a calculator
              Aren't you using JUnit 4? It's well worth the upgrade.

              Kaj
              • 5. Re: I can't understand JUnit technology...
                807601
                kajbj wrote:
                georgemc wrote:
                One simple example, to test a calculator
                Aren't you using JUnit 4? It's well worth the upgrade.

                Kaj
                Why? I could never see any benefit. What is the benefit? The only thing I could see was "not having to extend TestCase" any more, which I don't really see as much of a benefit
                • 6. Re: I can't understand JUnit technology...
                  791266
                  georgemc wrote:
                  kajbj wrote:
                  georgemc wrote:
                  One simple example, to test a calculator
                  Aren't you using JUnit 4? It's well worth the upgrade.

                  Kaj
                  Why? I could never see any benefit. What is the benefit? The only thing I could see was "not having to extend TestCase" any more, which I don't really see as much of a benefit
                  I like that it has annotations, and you can now use @After and @Before (same as setUp and tearDown) but you also have @BeforeClass and @AfterClass

                  Other nice stuff.

                  You can annotate....
                  1) ... a test with a timeout value, and an execution that exceeds that value is considered a failure.
                  2) ... and say that the test method is expected to throw a certain exception. It's a failure if it doesn't
                  3) ... a method with @Ignore. The runner will then ignore the method/result but still list the method as a test. (Much better than commenting out methods that are failing because the functionality hasn't been implemented yet)

                  I do also find the usage of assertThat easier to read, and finally, assumeThat is really good. The result of the rest of the asserts within the method are ignored if the assumeThat fails. (Good if you e.g have tests that only should be exeucted under certain conditions)
                  • 7. Re: I can't understand JUnit technology...
                    796254
                    All nice. I'm using it, but I have to admit that my preference would be to go to TestNG all the way. I think it is "next generation".

                    I'm prevented by convention and some limitations of Spring support. That last bit might have been corrected in 2.5; I haven't looked.

                    %