Junit Testing
Junit Testing
Contents:
• Introduction to jUnit testing
• Assertions
• Annotations
• Test case and Test Suite
• Evaluate Test out put
• Scrap Book
JUnit – the Java’s Unit Testing Framework
:: Introduction
JUnit was created by Erich Gamma and Kent Beck, two authors best known for Design
Patterns and eXtreme Programming, respectively.
Using JUnit you can easily and incrementally build a test suite that will help you
measure your progress, spot unintended side effects, and focus your development
efforts.
3
JUnit UML diagram
<Test>
run(TestResult)
TestCase
TestSuite
setUp
tearDown
JUnit – the Java’s Unit Testing Framework
:: Key JUnit Notions
The class TestCase has four important methods – run(), setUp(), tearDown() and
runTest().
import junit.framework.*;
import org.junit.Test;
public class TestAddition extends TestCase {
private int x = 1;
private int y = 1;
@Test public void addition()
{
int z = x + y;
assertEquals(2, z);
}
}
JUnit & Eclipse 10
Annotations
The @Ignore annotation says to not run a test
@Ignore("I don’t want Dave to know this doesn’t work")
@Test
public void add() {
assertEquals(4, program.sum(2, 2));
}
You shouldn’t use @Ignore without a very good
reason!
JUnit – the Java’s Unit Testing Framework
:: TestCase Class – SetUp
JUnit test runners automatically invoke the setUp() method before running
each Test Class.
This method typically initializes fields, turns on logging, resets environment
variables, and so forth, i.e. it sets up a context for the test cases to be
applied.
protected void setUp()
{
System.out.println("Before testing");
}
In JUnit 4, we can give it a more natural name and annotate it with @After.
@After protected void disposeObjects ()
{
System.out.println(“After testing");
System.gc();
}
Once the class we want to test, is created we can start with building the test cases.
To create a test case do
Home Work
a) Write code for class Fibbonachi and test with using Assertions.
b) Get error out put and evaluate
Test Suite
:: Test Suit - Introduction
We have performed tests on only one class, i.e. we have tested methods under the
consideration they belong to the same class.
In large projects we have many classes with methods that should be tested.
For testing multiple classes Eclipse and JUnit expose the concept of Test Suit.
A Test Suit is a collection of test cases that can be tested in a single batch.
A Test Suite is a simple way of running one program that, in turn, runs all test cases.
There are four ways to create a JUnit Test Suite Class. First, select the directory (usually
unittests/) that you wish to create the test suite class in.
Right click on a package in the Package Explorer view in the Java Perspective, and
select [Other... Java JUnit JUnit Test Suite].
You can create a normal Java class, but import the package junit.framework and
extend the TestSuite class.
Right click on the test suite class and select [Run As JUnit Test]
a) Create the test suite for test addition, division and Factorial.
b) Create the test suite for test Factorial and MinMax.
Home Work
1. Create Package named myAllTests
2. Create the test suite for test addition, division , Factorial , MinMax and Fibonacci.
Scrap Book
• Scrapbook pages allow you to evaluate and
test a small section of code without creating
an entire class.
– To evaluate a code fragment in the scrapbook
page, highlight an expression and select Display
from the pop-up menu.
– Add other classes or packages using Set Imports
Create a New Java Scrapbook Page
Home Work
1. Write the code snippets for test addition, division , Factorial , MinMax and
Fibonacci using scrap book.
Session expectations :
- Work with jUnit testing using:
Assertions
Annotations
Test case
Test Suite
Scrap Book