Skip to Content
Overview of JUnit

1 JUnit is a regression testing Framework written by Erick Gamma and Kert Beck
2 It is used by the developers who implements Unit tests in java
3 JUnit is a open Soure

Use of JUnit and its Advantages

1 JUnit tests are developers tests
2 JUnit tests allow the developer to write code faster with high quality.
3 Writing JUnit tests is inexpensive.
4 JUnit tests increase the stability of the software.
5 JUnit tests are written in Java
6 JUnit is free!

Design of JUnit

a)Textual Test
b)Graphical Test
Note: For Textual Test run the following class junit.textui.TestRunner junit.samples.Alltests ( All tests should pass with an OK) For Graphical Test run the following class junit.swingui.TestRunner junit.samples.Alltests (Green bar) If above doesn’t happen check with the classpath

JUnit are designed around two key patterns

1 Command Pattern
2 Composite pattern

A TestCase is a command object. Any class that contains test methods should subclass the TestCase Class. A TestCase can define any number of public testXXX () methods. When we want to check the expected and actual test results, we need to invoke a variation of the assert () method.

TestCase subclasses that contains multiple testxxx () methods can use the setup () and teardown () methods to initiate and release any common objects under test, referred to as the test fixture. Each test runs in the context of its own fixture, calling setup () before and teardown () after each test method to ensure there can be no side effects among test runs.

TestCase instances can be composed in to TestSuite hierarchies that automatically invoke all the testXXX () methods defined in each TestCase instance. A TestSuite is composite of the other tests, either TestCase instances or TestSuite instances. The composite behavior exhibited by the TestSuite allows us to assemble test suites of test suites of tests, to an arbitrary depth, and run all the tests automatically and uniformly to yield a single pass or fail status.

Installing JUnit

1 Download the Latest version of JUnit and JUnit Api ( for the developers reference)

2 Then Install the JUnit ( to install open the Zip file and add JUnit to the classpath)

3 Test the Installation (for testing we got two environments)

a) Textual Test
b) Graphical Test

Note:

1 For Textual Test run the following class junit.textui.TestRunner junit.samples.Alltests ( All tests should pass with an OK)

2 For Graphical Test run the following class junit.swingui.TestRunner junit.samples.Alltests (Green bar)

If above doesn’t happen check with the classpath

Writing a Test Case
1 Define a subclass of TestCase
2 Override the setup () method to initialize objects under test
3 Optionally override the teardown () method to release objects under test
4Define one or more public testXXX () methods that exercise the objects under test and assert the expected results.

Example for JUnit Test Case

Writing a Test Suite
1 Write a Test Suite that includes several test cases ( this allows to run of its test cases in one fell swoop) Steps
2 Write a Java class that defines a static suite () factory method that creates a test Suite containing all the tests.
3 Optionally define a main () method that runs the Test Suite in batch mode.

Example for JUnit Test Suite

Running the Test Case and Test Suite

Now that we’ve written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods.

JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly.

To run our test case using the textual user interface, use:

java junit.textui.TestRunner ShoppingCartTest The textual user interface displays “OK” if all the tests passed and failure messages if any of the tests failed.

To run the test case using the graphical user interface, use:

java junit.swingui.TestRunner ShoppingCartTest The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed. The Above ABCTestSuite can be run similarly: java junit.swingui.TestRunner ABCTestSuite

Things to be remembered before using JUnit

1 The software does well those things that the tests check.
2 Run all the tests in the system at least once per day (or night).
3 Write tests for the areas of code with the highest probability of breakage.
4 Write tests that have the highest possible return on your testing investment.
5 If you find yourself debugging using System.out.println(), write a test to automatically check the result instead.
6 When a bug is reported, write a test to expose the bug.
7 Write unit tests before writing the code and only write new code when a test is failing

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

    1. Markus Kohler
      Hi,
      >Any experience trying to use JUnit in conjuction with SAP Portal?

      Sure. There’s no problem using Junit inside the portal.
      You can write your own Testrunner, that allows you to run tests from within the portal.

      There’s also an opensource Testrunner http://www.junitee.org/ which outputs HTML and a provides servlet which can be used as an entry point to your test cases. This application should run within the portal, also I haven’t tried it out yet.

      Apache Cactus is another framework that extends Junit to more easily allow you to test Servlets :
      http://jakarta.apache.org/cactus/integration/howto_tomcat.html

      Regards,
      Markus

      Markus

      (0) 

Leave a Reply