Junit Training: Chris Yeung 8 Sept, 2006
Junit Training: Chris Yeung 8 Sept, 2006
Introduction
JUnit is a regression testing framework Written by Erich Gamma and Kent Beck.
What is Junit?
Test framework provides tools for:
assertions running tests aggregating tests (suites) reporting results
Test infected
Its a Good Thing, no penicillin needed Immediate gratification with build iterations
Start with The Simplest Thing That Could Possibly Work.
Iterate by successive application of design pattern.
Break the cycle of more pressure == fewer tests Reduce code captivity
If others can test it, others can work on it.
Junit Mechanics
Define a subclass of TestCase. Override the setUp() & tearDown()methods. Define one or more public testXXX()methods
Exercise the object(s) under test. Asserts the expected results.
Junit Mechanics
Simple Testcase
public class StringTest extends TestCase { protected void setUp(){ /* run before */} protected void tearDown(){ /* after */ } public void testSimpleAdd() { String s1 = new String(abcd); String s2 = new String(abcd); assertTrue(Strings not equal, s1.equals(s2)); } public static void main(String[] args){ junit.textui.TestRunner.run (suite ()); } }
<JUnit Report>
assertEquals(expected, actual) assertEquals(String message, expected, actual) This method is heavily overloaded: arg1 and arg2 must be both objects or both of the same primitive type For objects, uses your equals method, if you have defined it properly, as public boolean equals(Object o)--otherwise it uses == assertSame(Object expected, Object actual) assertSame(String message, Object expected, Object actual) Asserts that two objects refer to the same object (using ==) assertNotSame(Object expected, Object actual) assertNotSame(String message, Object expected, Object actual) Asserts that two objects do not refer to the same object
Fixtures
Handle common objects under test setup() and tearDown() used to initialize and release common objects. Used to insure there are no side effects between tests. Enforce the test independence rule, test execution order is not guarunteed.
Execrise
Write a testcase to test 3 method of java.util.ArrayList
Test Suites
public static void main (String [] args){ junit.textui.TestRunner.run (suite ()); }
public static Test suite (){ suite = new TestSuite ("AllTests"); suite.addTest (new TestSuite (AllTests.class)); suite.addTest (StringTest.suite());
public void testAllTests () throws Exception{ assertTrue (suite != null); } }
TestRunners
Text
Lightweight, quick quiet Run from command line
java StringTest ....... Time: 0.05
Tests run: 7, Failures: 0, Errors: 0
TestRunners - Swing
Run with java junit.swingui.TestRunner
Factory pattern
Provides for abstraction of creation of implementations from the tests.
Strategy pattern
Because FactoryFinder dynamically resolves desired factory, implementations are plugable
Of course, you can write mock yourself by implement interface with simple implementation
Separate production and test code But typically in the same packages Compile into separate trees, allowing deployment without tests Dont forget OO techniques, base classing Test-driven development
1. 2. 3. 4. 5. Write failing test first Testing for Exceptions Test then Fix Test then Refactor Where should I put my test files?
Tests help you make sure you dont break it while improving it.
Small change, test, small change, test...
Resources
http://www.junit.org http://www.xprogramming.com http://www106.ibm.com/developerworks/java/library /j-junitmail/index.html http://jakarta.apache.org/ant