Anda di halaman 1dari 20

Test Driven Development

XP is based on Test-Driven Development :

A software development technique that involves repeatedly
first writing a test case and then implementing only the code
necessary to pass the test

JUnit is that kind of tool helps Java programmers to so .

Why JUnit?
Without JUnit, you will have to use println() to print
out some result :
No explicit concept of test passing or failure

No mechanism to collect results in a structured fashion

No replicability

JUnit is a API for easily creating Java test cases

-Test runners for running tests
-Aggregation facility (test suites)

How to write JUnit-based tests ?

This tutorial is based on JUnit 4.x

Include JUnit.jar (latest version 4.x) in the classpath

Define a Class (Test Case)
Define one or more public test_method_name( ) methods in the
class and decorate them with @Test tag
Implement the body of test_method_name( ) methods()
Exceute TestCase

Test Methods
Test methods can have arbitrary name but annotated
with @Test

Test methods must have no arguments

Test methods are type of void
Use Assertions in the body of test methods

JUnit Assertions
assertTrue(boolean condition) , assertFalse(boolean condition)
assertEquals(Object expected, Object actual)
Uses equals() comparison, Overloaded for all primitive types

assertSame(Object expected, Object actual)

assertNotSame(Object expected, Object actual)
Uses == comparison

assertEquals(float expected, float actual, float tolerance)

assertNull(Object o), assertNotNull(Object o)

More .... look at JUnit API


Sample Test Class

public class TestBiblioteket {
BiblioDocument bibDoc;
* Tests should begin with the @Test tag.

public void documentCreationTest() {
bibDoc = new BiblioDocument ("a", "t", "1999");
assertEquals("t", bibDoc.getTitle());
assertEquals("1999", bibDoc.getYear());
bibDoc = null;

public class BiblioDocument {
private String author;
private String title;
private String year;
public BiblioDocument() { }
public BiblioDocument (String author, String title, String year) { = author;
this.title = title;
this.year = year;
public String getAuthor() {
public String getTitle() {
return this.title;
public String getYear() {
return this.year;

Run Test Case- Success

>java -classpath junit4.x.jar org.junit.runner.JUnitCore


Time : 0.001
OK (1 test)

JUnit for Eclipse


JUnit Basic Concepts

Test fixture : Common set of test data and
collaborating objects shared by many test
methods, implemented as instance variables in
the test class (e.g. BiblioDocument bibDoc) also called
Test Context

Test case: defines a fixture to run a related set

of tests. Typically every class you develop
should have a test case (TestBiblioteket class).
Test suite: is a collection of related Test Cases.

Test Suit
Use @RunWith, is designed to facilitate having different test
Use @SuiteClasses, which takes as a parameter a list of
classes intended to represent the test suite.

@SuiteClasses( { TestBibliotekt.class } )

public class MyJUnitSuit {



More Annotations
The purpose of the following annotations (fixtures) is to
setup a known environment to run tests so the results
would be predictable.
You can setup the fixtures in two levels:
Method Level using : @Before, @After
These methods executed before and after each test method

Class Level using : @BeforeClass, @AfterClass

These methods executed once : in the beginning ,when the test
case starts and in the end, when the test case is ended.

Method Level Fixtures

public class TestBiblioteket {
BiblioDocument bibDoc;

public void initDocument() {
bibDoc = new BiblioDocument ("a", "t", "1999");
public void documentCreationTest() {
assertEquals("t", bibDoc.getTitle());
assertEquals("1999", bibDoc.getYear());
public void destroyDocument() {
bibDoc = null;


More Test Cases

public class TestResult {
public void EmptyResultTest() {
Result r = new Result();
assertEquals(0, r.getCount());
public void ResultWithTwoDocumentsTest() {
BiblioDocument d1 = new BiblioDocument("a1", "t1", "1999");
BiblioDocument d2 = new BiblioDocument("a2", "t2", "2000");
Result r = new Result (new BiblioDocument[]{d1,d2});
assertSame(r.getItem(0) , d1);
assertSame(r.getItem(1) , d2);

More Class
public class Result {
private BiblioDocument[] colllection = new BiblioDocument[0];

public Result() { }
public Result(BiblioDocument[] collection) {
this.colllection = collection;
public int getCount() {
return this.colllection.length;
public BiblioDocument getItem(int i) {
return this.colllection[i];

Testing collection of related test classes


@SuiteClasses ({ TestBibliotekt.class , TestResult.class })
public class MyJUnitSuit {


1. JUnit :Philosophy, Mechanics, and Best Practices,
by: Erik Hatcher,
2. JUnit Tutorial by Hong Qing Yu , Nov 2005
3. Java Tools for eXtreme programming book




More materials will be uploaded to the course web

page or sent to mailing list.
JUnit support in Eclipse , NetBeans ,..

..... And more just Google Junit



Good Luck !