Nimal blogs here
27 Apr 2008
Ref:
JUnit is a simple, open source framework to write and run repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:
Tests should be written before the code. Test-first programming is practiced by only writing new code when an automated test is failing.
Good tests tell you how to best design the system for its intended use. They effectively communicate in an executable format how to use the software. They also prevent tendencies to over-build the system based on speculation. When all the tests pass, you know you're done!
Whenever a customer test fails or a bug is reported, first write the necessary unit test(s) to expose the bug(s), then fix them. This makes it almost impossible for that particular bug to resurface later.
Test-driven development is a lot more fun than writing tests after the code seems to be working. Give it a try!
import org.junit.*;import static org.junit.Assert.*;
public class SampleTest {
private java.util.List emptyList;
/** * Sets up the test fixture. * (Called before every test case method.) */ @Before public void setUp() { emptyList = new java.util.ArrayList(); }
/** * Tears down the test fixture. * (Called after every test case method.) */ @After public void tearDown() { emptyList = null; }
@Test public void testSomeBehavior() { assertEquals("Empty list should have 0 elements", 0, emptyList.size()); }
@Test(expected=IndexOutOfBoundsException.class) public void testForException() { Object o = emptyList.get(0); }}
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:
@Test(expected=IndexOutOfBoundsException.class) public void testIndexOutOfBoundsException() { ArrayList emptyList = new ArrayList(); Object o = emptyList.get(0); }
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.
The following is an example test that fails when the IndexOutOfBoundsException is raised:
@Test public void testIndexOutOfBoundsExceptionNotRaised() throws IndexOutOfBoundsException {
ArrayList emptyList = new ArrayList(); Object o = emptyList.get(0); }
This is a list of the different types of assertion statements that are used to test your code. Any Java data type or object can be used in the statement. These assertions are taken from the JUnit API.
Ref:
public class RangeTest extends TestCase { private Range mRange; public RangeTest(String name) { super(name); }
protected void setUp() throws Exception { super.setUp(); mRange = new Range(1, 5); }
public void testConstructor() { // pre: aLower < aUpper try { new Range(6, 4); fail("[6,4] should be precluded"); } catch (AssertionError e) { } // pre: aLower == aUpper new Range(-3, -3); // post: aLower == getLower() Assert.assertEquals("[1,5].getLower() should be 1", 1, mRange.getLower()); // post: aUpper == getUpper() Assert.assertEquals("[1,5].getUpper() should be 5", 5, mRange.getUpper()); }
public void testGetLower() { // pre: object constructed Assert.assertNotNull("[1,5] could not be constructed", mRange); // post: getLower() == aLower provided to c'tor Assert.assertEquals("[1,5].getLower() should be 1", 1, mRange.getLower()); }
public void testGetUpper() { // pre: object constructed Assert.assertNotNull("[1,5] could not be constructed", mRange); // post: getUpper() == aUpper provided to c'tor Assert.assertEquals("[1,5].getUpper() should be 5", 5, mRange.getUpper()); }
public void testIntersects() { // pre: anOther != null try { mRange.intersects(null); fail("intersects(null) should be precluded"); } catch (AssertionError e) { } // post: true if two ranges have an integer in common Assert.assertTrue(!mRange.intersects(new Range(-3, -1))); Assert.assertTrue(!mRange.intersects(new Range(6, 7))); Assert.assertTrue(!mRange.intersects(new Range(-2, 1))); Assert.assertTrue(!mRange.intersects(new Range(5, 7))); Assert.assertTrue(mRange.intersects(new Range(0, 2))); Assert.assertTrue(mRange.intersects(new Range(3, 4))); Assert.assertTrue(mRange.intersects(new Range(4, 8)));}}
Ref: http://www.hacknot.info/hacknot/action/showEntry?eid=17
Recent Comments