JUnit Getting Started

A beginners guide with complete code example.

Main objective: Write a test case using JUnit as simple as possible.
Approach: First write a class, then write the TestCase and the TestSuite.
Readers:   Should have some experience or knowledge in Java
JUnit is a regression testing framework written by Erich Gamma and Kent Beck. This is used by developers to implement unit tests in Java.

Why unit testing is needed?

With unit testing the end product is completely checked for the correcteness of the code. With the time, the set of tests grow and the ease of testing improves. One of the most important things is that developers can be confident on the code and less time needed for debugging.

What is JUnit?

JUnit is a Test framework. The features available are;
  • Assertion methods (test methods)
  • Running tests (TestRunner)
  • Aggregating tests (test suites)
  • Reporting results (text based and GUI based)
One factor that developers may worry is : the developers has to write tests.

Writing a JUnit Test in 5 minutes

First add the junit.jar file to the project (All the following classes are tested using junit-3.8.1.jar). Then write the follwing classes.

Writing JUnit class

  1. Define a subclass of TestCase.
  2. Override the setUp() & tearDown()methods.
  3. Define one or more public testXXX()methods
    - all the methods starting with test will be run by the TestRunner
    • call the methods of tested object
    • check the expected results with assertXXX() methods
  4. Define a static suite() factory method
  5. Create a TestSuite class containing all the tests.
  6. Optionally define main() to run the TestCase in batch mode.

1. Write a class - Calc.java

package com.myproj;

public class Calc {

    public Calc() {
        super();
    }
    public int add(int a, int b){
        // errorneous method
        return a+b+1;
    }
    public int multiply(int a, int b){
        return a*b;
    }
}

2. Write the test case - TestCalc.java (anyname can be used)

package test.com.myproj;

import junit.framework.TestCase;
import com.myproj.Calc;//testing class

public class CalcTest extends TestCase {

    Calc c = null;

    public CalcTest(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        super.setUp();
        c = new Calc();
    }

    /**
     * Test method for com.myproj.Calc.add(int, int)
     */
    public void testAdd() {
        int x = c.add(5,6);
        assertEquals(11, x);
    }

    /**
     * Test method for com.myproj.Calc.multiply(int, int)
     */
    public void testMultiply() {
        int x = c.multiply(5,6);
        assertEquals(30, x);
    }

    public static void main(String[] args) {
        junit.textui.TestRunner.run(CalcTest.class);
    }
}
  • Run test case using below command.
    java test.com.myproj.CalcTest
    
  • The line junit.textui.TestRunner.run(CalcTest.class) is where the trick occurs. The TestRunner runs all the methods in the CalcTest class which has testXXX() signature using reflection.

  • c.add(5,6) line should return 11 if the method works fine. The return value is checked against the expected value in the assertXXX() method. As the method is errorneous, this throws junit.framework.AssertionFailedError. But the multiply() method works fine and causes no errors.

3. Write the Test Suite - Tests.java (anyname can be used)

package test.com.myproj;

import junit.framework.Test;
import junit.framework.TestSuite;

public class Tests {
    public static void main(String[] args) {
       junit.textui.TestRunner.run(Tests.class);
       //junit.swingui.TestRunner.run(Tests.class);
       //junit.awtui.TestRunner.run(Tests.class);
    }
    public static Test suite() {
       TestSuite suite = new TestSuite("Test for test.com.myproj");
       //$JUnit-BEGIN$
       suite.addTestSuite(CalcTest.class);
       suite.addTestSuite(ComputerTest.class);//another test case
       //$JUnit-END$
       return suite;
    }
}
  • Run Tests using below command.
    java test.com.myproj.Tests
    
    all the testXXX() methods in all the testCases which are added to TestSuite inside suite() method are called.
  • junit.textui.TestRunner.run() gives results in text mode. But if junit.swingui.TestRunner.run() or junit.awtui.TestRunner.run() used, results come in UI mode.
  • Add the TestCase to the TestSuite inside suite() method, when ever a new TestCase is needed.
Oh! you are done. The complete TestSuite is completed.
One important thing to keep in mind, write code to do small operations as much as possible.

If you have any questions or comments, just add as a comment below and will be here to help you guys.

References:
www.junit.org
www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html
www.admc.com/blaine/howtos/junit/junit.html

COMMENTS

BLOGGER: 9
Loading...
Name

About,2,Adsense,3,Ant,1,Apache,3,Axis,3,Blogger,1,Books,1,CentOS,2,Chrome,2,CSS,2,Database,3,Earn Online,3,Eclipse,10,Facebook,1,Firefox,10,Gmail,4,GNU/Linux,9,Google,26,GWT,8,Hardware,2,IE,5,Interesting,15,Internet,14,Java,49,Javascript,7,JBoss,1,Jenkins,1,Log4j,2,Me,6,Microsoft,2,Miscellaneous,1,News,11,Opinion,10,OSGi,1,PHP,1,Productivity,3,Programming,36,Puzzle,3,Security,4,Software,41,Sports,9,Spring,2,Story,6,Subversion,3,TDD,4,Tech,2,Tips,1,Tomcat,6,Tutorial,13,Ubuntu,4,Web application,14,Web Design,2,Web services,3,Windows,10,Yahoo,1,Zip,2,
ltr
item
Digizol: JUnit Getting Started
JUnit Getting Started
A beginners guide with complete code example.
https://3.bp.blogspot.com/-3qU6nKY9Oh8/We28IHvnloI/AAAAAAAACjY/xv9zjkV-5ygr4wKZEYqhxifUwkuUHEBcACLcBGAs/s1600/Junit%2BGetting%2BStarted%2B3.8.1%2Bwww.digizol.com.jpg
https://3.bp.blogspot.com/-3qU6nKY9Oh8/We28IHvnloI/AAAAAAAACjY/xv9zjkV-5ygr4wKZEYqhxifUwkuUHEBcACLcBGAs/s72-c/Junit%2BGetting%2BStarted%2B3.8.1%2Bwww.digizol.com.jpg
Digizol
http://www.digizol.com/2006/06/junit-getting-started.html
http://www.digizol.com/
http://www.digizol.com/
http://www.digizol.com/2006/06/junit-getting-started.html
true
7440473
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy