Introduction to JUnit 3.8 using NetBeans IDE by ewghwehws


									Introduction to JUnit 3.8
             SEG 3203
             Winter ‘07

            Prepared By
          Samia Niamatullah
 JUnit is a simple, open source framework to
  write and run repeatable tests. JUnit features
    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
       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
               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

 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
 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
    }                                              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.
        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

 setUp() and tearDown() methods can be used to
  define fixtures
 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.

 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
 Failure
    A test fails when an assert is violated or a fail
     statement is reached.
 Error
    An unexpected exception is thrown or timeout
             Status of a Test
 On failure and error, the test results also show a stack
trace of the execution.
   LAB-5110 NetBeans™: JUnit (April 2005)

   Unit Testing in Eclipse Using JUnit by Laurie Williams, Dright Ho, and
    Sarah Smith (

   JUnit Testing With Netbeans

   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

To top