JUnit 4.x – Testing Framework : Introduction to JUnit
- JUnit Introduction
- Features of JUnit
- Design of JUnit
- TestNG Vs JUnit
- Understanding Unit Test
- Sample Code for JUnit
JUnit Introduction |
What is JUnit
- JUnit supports Unit testing for Java Programming Language
- JUnit is an open source, simple and provides a testing frame work for Java.
- The framework establishes relationship between Development and Testing
- JUnit is a freeware
- JUnit is integrated with IDEs (Eclipse, JBuilder, etc…) and Ant
- Sun Microsystems has not yet included a separate package for JUnit in JDK.
- JUnit uses “junit.jar” file
History of JUnit
- In mid-90’s, Kent Beck developed xUnit automated test tool for Smalltalk.
- It was known as SUnit
- Erich Gamma and Kent Beck developed JUnit.
- JUnit is one of the xUnit testing frameworks.
- JUnit 2.0 was the first effective release with 14 classes in it
- Most popularly used JUnit version is version 3.8.1
- Latest version of JUnit is 4.1 with 41 classes.
Uses of JUnit
- JUnit automates the developer’s work
- Automated tests will improve the performance
- JUnit reduces human error and propagation of errors.
Features of JUnit |
- JUnit helps to write test cases and test suites for testing java application for verifying the desired results we use Assertions.
- For sharing common test data Test fixtures can be used.
- With the help of Test suites we can organize and run tests easily.
- We can use both Textual as well as Graphical test runners.
- It supports unit testing of the code during the development cycle itself.
Design of JUnit |
Design of JUnit
JUnit is designed around two key design patterns: the Command pattern and the 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 you want to check the expected and actual test results, you invoke a variation of the assert() method.
TestCase subclasses that contain multiple testXXX() methods can use the setUp() and tearDown() methods to initialize 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 into TestSuite hierarchies that automatically invoke all the testXXX() methods defined in each TestCase instance. A TestSuite is a composite of other tests, either TestCase instances or other TestSuite instances. The composite behavior exhibited by the TestSuite allows you 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.
TestNG Vs JUnit |
TestNG Vs JUnit
JUnit 4 and TestNG are both very popular unit test framework in Java technology. Both frameworks look very similar in functionality. Which one is better? Which unit test framework should I use in Java project?
Now You decide!!!
Understanding Unit Test |
Unit Testing
- Unit testing is intended to check the functionality of a particular class.
- Unit testing is done by programmers
- Tests other than unit testing are done by testers.
-
Unit testing has to be completed before :
- Module testing,
- Functional testing,
- System testing, etc…
Types of Unit Test
- XUnit architecture introduced automated unit testing
- There are many Unit testing frameworks for different programming languages
-
Few of the units testing frame works are:
- JAVA – Junit, TestNG
- C – CUnit,
- C++ – CppUnit, Python (PyUnit), Fortran (fUnit),
- .Net -.NUnit etc…
- xUnit Architecture was first implemented for java
- It is known as JUnit.
Sample Code for JUnit |
Foo.java
package com.javaskool;
public class Foo {
private int[] array;
public Foo(int size) {
array= new int[size];
}
public String getHelloWorld() {
return "Hello World";
}
public boolean getTruth() {
return true;
}
public void setElement(int position, int value) throws ArrayIndexOutOfBoundsException {
array[position] = value;
}
public int getElement(int position) throws ArrayIndexOutOfBoundsException {
return array[position];
}
}
Foo.java
package com.javaskool;
import junit.framework.*;
//import junit.textui.*;
public class FooTest extends TestCase
{
public Foo Fo;
protected int testSize;
public FooTest(String name)
{
super(name);
}
protected void setUp()
{
testSize = 3;
Fo = new Foo(testSize);
}
/**
* test original method getHelloWorld, example of Assert.assertEquals
*/
public void testGetHelloWorld() {
System.out.println("testGetHelloWorld");
String result = "Hello world";
Assert.assertEquals(result, Fo.getHelloWorld());
}
public void testGetTruth() {
System.out.println("testGetTruth");
Assert.assertTrue(Fo.getTruth());
}
public void testSetElement() {
try {
System.out.print("testSetElement, exception threw : >> ");
Fo.setElement(testSize, 0);
Assert.fail("setElement should raise an Exception");
}
catch (Exception e)
{
System.out.println(e.toString());
}
try {
System.out.println("testSetElement, OK ");
int position = testSize - 1;
int result = 10;
Fo.setElement(position, result);
Assert.assertEquals(result, Fo.getElement(position));
}
catch (Exception e)
{
System.out.println(e);
}
}
public void testGetElement() {
}
public static Test suite() {
return new TestSuite(FooTest.class);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
// to use GUI interface (no Suite paramater permitted)
// junit.swingui.TestRunner.run(FooTest.class);
}
}
Recent Comments