Java Jun It
Java Jun It
Topics
What is and Why JUnit framework? JUnit mechanics Fixtures Test suites Test runners JUnit classes Best practice guidelines Design patterns for testing Extensions
Acknowledgment
This presentation is a modified (and enhanced) version of Orlando Java User's Group presentation on the same subject by Matt Weber
Why test?
Automated tests prove features Tests retain their value over time and allows others to prove the software still works (as tested). Confidence, quality, sleep Get to code sooner by writing tests.
Unit Testing
Is a procedure used to validate that individual units of source code are working properly A unit is the smallest testable part of an application
In procedural programming a unit may be an individual program, function, procedure, web page, menu etc, while in object-oriented programming, the smallest unit is always a Class; which may be a base/super class, abstract class or derived/child class
What is JUnit?
JUnit is a regression testing framework Used by developers to implement unit tests in Java (de-facto standard) Integrated with Ant
Testing is performed as part of nightly build process
Goal: Accelerate programming and increase the quality of code. Part of XUnit family (HTTPUnit, Cactus), CppUnit
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
Test methods
Test methods has name pattern testXXX()
XXX reflects the method of the target class
Test methods must have no arguments Test methods are type of void
// Add testing methods public void testSimpleAdd() { String s1 = new String(abcd); String s2 = new String(abcd); assertTrue(Strings not equal, s1.equals(s2)); } // Could run the test in batch mode public static void main(String[] args){ junit.textui.TestRunner.run (suite ()); }
Assert Statements
20
Assert statements
JUnit Assertions are methods starting with assert Determines the success or failure of a test An assert is simply a comparison between an expected value and an actual value Two variants
assertXXX(...) assertXXX(String message, ...) - the message is displayed when the assertXXX() fails
Assert statements
Asserts that a condition is true assertTrue(boolean condition) assertTrue(String message, boolean condition) Asserts that a condition is false assertFalse(boolean condition) assertFalse(String message, boolean condition)
Assert statements
Asserts expected.equals(actual) behavior assertEquals(expected, actual) assertEquals(String message, expected, actual) Asserts expected == actual behavior assertSame(Object expected, Object actual) assertSame(String message, Object expected, Object actual)
Assert statements
Asserts object reference is null assertNull(Object obj) assertNull(String message, Object obj) Asserts object reference is not null assertNotNull(Object obj) assertNotNull(String message, Object obj) Forces a failure fail() fail(String message)
Fixtures
25
Fixtures
setUp() and tearDown() used to initialize and release common test data
setUp() is run before every test invocation & tearDown() is run after every test method
Example: setUp
public class MathTest extends TestCase { protected double fValue1, fValue2; protected void setUp() { fValue1= 2.0; fValue2= 3.0; } public void testAdd() { double result= fValue1 + fValue2; assertTrue(result == 5.0); } public void testMultiply() { double result= fValue1 * fValue2; assertTrue(result == 6.0); }
Test Suites
28
Test Suites
Used to collect all the test cases Suites can contain testCases and testSuites
TestSuite(java.lang.Class theClass, <java.lang.String name>) addTest(Test test) or addTestSuite(java.lang.Class testClass) Suites can have hierararchy
Test Runners
32
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
JUnit Classes
37
Best Practice
39
Factory pattern
Provides for abstraction of creation of implementations from the tests.
Strategy pattern
Because FactoryFinder dynamically resolves desired factory, implementations are plugable
Extensions
50
JUnit Extensions
JUnitReport Cactus JWebUnit XMLUnit MockObject StrutsTestCase
JUnitReport
Apache Ant extension task Uses XML and XSLT to generate HTML
JWebUnit
Framework that facilitates creation of acceptance tests for web applications
XMLUnit
Provides an XMLTestCase class which enables assertions to be made about the content and structure of XML
Differences between two pieces of XML Validity of a piece of XML Outcome of transforming a piece of XML using XSLT Evaluation of an XPath expression on a piece of XML
Mock Objects
Generic unit testing framework whose goal is to facilitate developing unit tests in the mock object style What is a Mock object?
"double agent" used to test the behaviour of other objects Dummy object which mimics the external behaviour of a true implementation observes how other objects interact with its methods and compares actual behaviour with preset expectations
StrutsTestCase
Extends the JUnit TestCase class that Provides facilities for testing code based on the Struts framework You can test
implementation of your Action objects mappings declarations form beans declarations forwards declarations
58