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)
-Reporting

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
3

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
4

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


5

Sample Test Class


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

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

BiblioDocument
public class BiblioDocument {
private String author;
private String title;
private String year;
public BiblioDocument() { }
public BiblioDocument (String author, String title, String year) {
this.author = author;
this.title = title;
this.year = year;
}
public String getAuthor() {
return this.author;
}
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
mypackage.TestBiblioteket

Output:

Time : 0.001
OK (1 test)

JUnit for Eclipse

10

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.
11

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

@RunWith(Suite.class)
@SuiteClasses( { TestBibliotekt.class } )

public class MyJUnitSuit {


}

12

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.
13

Method Level Fixtures


public class TestBiblioteket {
BiblioDocument bibDoc;

@Before
public void initDocument() {
bibDoc = new BiblioDocument ("a", "t", "1999");
}
@Test
public void documentCreationTest() {
assertEquals("a",bibDoc.getAuthor());
assertEquals("t", bibDoc.getTitle());
assertEquals("1999", bibDoc.getYear());
}
@After
public void destroyDocument() {
bibDoc = null;
}
}

14

More Test Cases


public class TestResult {
@Test
public void EmptyResultTest() {
Result r = new Result();
assertEquals(0, r.getCount());
}
@Test
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);
}
}
15

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];
}
}
16

Testing collection of related test classes


(TestSuite)

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

17

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

18

More

Links

More materials will be uploaded to the course web


page or sent to mailing list.

http://www.junit.org/index.htm
http://open.ncsu.edu/se/tutorials/junit/
http://www.cs.umanitoba.ca/~eclipse/10-JUnit.pdf
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsyste
m/build/tutorials/junit/junit.pdf
http://junit.sourceforge.net/javadoc/junit/framework
JUnit support in Eclipse , NetBeans ,..
http://www.laliluna.de/eclipse-junit-testing-tutorial.html
http://www.scribd.com/doc/524491/JUnit-Tutorial
http://www.fsl.cs.sunysb.edu/~dquigley/cse219/index.php?it=netbeans&tt=junit&pf=y

..... And more just Google Junit


19

Questions?

Good Luck !
20