Introduction to JUnit 3.8 using NetBeans IDE

Document Sample
Introduction to JUnit 3.8 using NetBeans IDE Powered By Docstoc
					Introduction to JUnit 3.8
             SEG 3203
             Winter ‘07


            Prepared By
          Samia Niamatullah
                      JUnit
 JUnit is a simple, open source framework to
  write and run repeatable tests. JUnit features
  include:
    Assertions for testing expected results
    Test fixtures for sharing common test data
    Test runners for running tests

 JUnit was originally written by Erich Gamma and
  Kent Beck.

 Used for unit testing in Java. Other xUnit
  frameworks exist for other languages.

 Supported by www.junit.org
       Earlier Unit Testing
 Use a debugger to go through code line by line.
    Good to locate bugs.
    Difficult for large system.
    Impossible for regression testing.


 Insert output statements in code
    Easy to implement.
    Rely on human eyes to recognize problems.
    Hard to organize… causes the dreaded "Scroll
     Blindness".
               Unit Testing
 Programmers have aversion towards writing
  testing code
   Too busy.
   It is difficult to write and maintain.


  Greater need to automate and simplify the
  process of writing unit tests
      How Can JUnit help?
 JUnit automates testing.
     In fact, the organization and execution of testing.


 JUnit saves 50% of your work, not all.
     Still need to design test cases and implement it.
     Fortunately, those are all needed to be done.


   Write tests once, but run them as often needed.
   Provides a well designed framework.
   Abstracts common concerns of unit testing.
   Easy to integrate test cases and test suites.
 Test Method vs. Test Case
 Test Case
    A test case is usually a single run of a specific
     functionality.


 Test Method
    A test method can contain one or more test cases.
    A test method usually has one or more assert
     statements or fail statements.


 Test Suit
    A collection of of test cases/classes executed together
        Code Under Test
 Suppose we want to implement a basic calculator
  program.
 It has the general math operations- add, subtract,
  multiply, divide and square root.
 Methods take a single parameter n and perform the
  operation with the stored value result.

   public class Calculator {
      private int result;
       public void add(int n) {
               result = result + n;
           }
       public void substract(int n) {
               result = result - n;
           }
    Anatomy of the Test Class
import junit.framework.*;
public class CalculatorTest extends TestCase {
                                                   The test class should extend
    public CalculatorTest(String testName) {       junit.framework.TestCase
      super(testName);
    }                                              Tests must be public void
    protected void setUp() throws Exception {
    }                                              All test methods should have a
    protected void tearDown() throws Exception {   name start with “test”
    }
                                                   All source methods in the class
                                                   under test must be public or
public void testAdd() {                            protected, not private, in order to
    System.out.println("add");                     be tested by JUnit.

        int n = 0;                                 If the method in the class under
        Calculator instance = new Calculator();    test is private, the test class must
                                                   be in the same package.
        instance.add(n);
        fail("The test case is a prototype.");
    }

}
    Anatomy of the Test Class
   import junit.framework.*;
      The classes needed for using the assert methods.

   public class CalculatorTest extends TestCase {
      The use of the syntax prefixes can be avoided by extending
       the TestCase class.

   setUp(), tearDown()
      Run to do some common things that are done before and
       after each test
      For example, in calculator example, we might like to reset the
       calculator to zero before and after testing each operation.

   assert*() methods
      Used to compare the obtained and expected results
      The failure/success of the tests depend on their outcome.
              Test Fixture
 Useful if you have two or more tests for a
  common set of objects.

 Avoids duplicating the code necessary to
  initialize (and cleanup) the common objects.

 Tests don't share the state of objects in the test
  fixture.

 setUp() and tearDown() methods can be used to
  define fixtures
        junit.framework.Assert
 Provide static methods which can help comparing
  the expected result and actual result.
 If any assert is violated, a failure will be recorded.

assertEquals (expected, actual)      assertEquals (message, expected, actual)
assertSame (expected, actual)        assertSame (message, expected, actual)
assertNotSame (unexpected, actual)   assertNotSame (message, unexpected, actual)
assertFalse (condition)              assertFalse (message, condition)
assertTrue (condition)               assertTrue (message, condition)
assertNotNull (object)               assertNotNull (message, object)
assertNull (object)                  assertNull (message, object)
fail ()                              fail (message)
             Test Execution
 Execute a test by using the Run function of the IDE.
 NetBeans/Eclipse, can use a default test runner-- all the
  tests in the class run one by one.

Alternatively,
 Manually control the execution of the test program and
   select which test classes/methods to run by including the
   following method:

   public static Test suite( ) {
       TestSuite suite = new TestSuite( );
       suite.addTest(new <class name>(“<method name>”) );
       …………………………………
       return suite;
   }
            Status of a Test
 A test is a single run of a test method.

 Success
    A test succeeds in time when No assert is violated; No
     fail statement is reached; No unexpected exception is
     thrown.
 Failure
    A test fails when an assert is violated or a fail
     statement is reached.
 Error
    An unexpected exception is thrown or timeout
     happens.
             Status of a Test
 On failure and error, the test results also show a stack
trace of the execution.
                      References
   LAB-5110 NetBeans™: JUnit (April 2005)
    (http://developers.sun.com/events/techdays/self_paced_labs.jsp)

   Unit Testing in Eclipse Using JUnit by Laurie Williams, Dright Ho, and
    Sarah Smith (http://open.ncsu.edu/se/tutorials/junit/#section1_0)

   JUnit Testing With Netbeans
    (http://www.fsl.cs.sunysb.edu/~dquigley/cse219/index.php?it=netbean
    s&tt=junit&pf=y)

   JUnit 4 Tutorial by Ji Chao Zhang, October 23, 2006 (CSI 5111
    presentation) Based on “Get Acquainted with the New Advanced
    Features of JUnit 4” by Antonio Goncalves

   JUnit Test Infected: Programmers Love Writing Tests; Kent Beck,
    Erich Gamma.

   JUnit FAQ Edited by Mike Clark
    (http://junit.sourceforge.net/doc/faq/faq.htm#overview_1)

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:16
posted:7/23/2012
language:English
pages:15