Professional Documents
Culture Documents
Why JUnit?
Without JUnit, you will have to use println() to print
out some result :
No explicit concept of test passing or failure
Test Methods
Test methods can have arbitrary name but annotated
with @Test
JUnit Assertions
assertTrue(boolean condition) , assertFalse(boolean condition)
assertEquals(Object expected, Object actual)
Uses equals() comparison, Overloaded for all primitive types
@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;
}
}
Output:
Time : 0.001
OK (1 test)
10
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 } )
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
@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 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
@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
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
Questions?
Good Luck !
20