Unit Testing
Unit Testing
[email protected]
Australian Development Centre
Brisbane, Australia
Aims
Isolatable
Repeatable
Automatable
Easy to Write
Why Unit Test?
Faster Debugging
Faster Development
Better Design
Excellent Regression Tool
Reduce Future Cost
Unit Tests
GUI testing
– Marathon Man, WinRunner
EJB Components
– HttpUnit, Cactus
Limited Reporting mechanism
– Artima
Time to set up
Testing of non-java objects difficult
Key Concepts in JUnit
Test interface
<Test>
Assert Assert
run(TestResult)
TestCase
– assertTrue
– assertEquals TestCase
TestSuite
setUp()
– fail tearDown()
run(TestResult)
TestSuite
TestDecorator/TestSetup
Failures vs Errors
JUnit is Easy
…
public void testInvalidPersonName()
{
person.setFirstName(null);
person.setLastName(“Smith”);
try
{
personService.createPerson(person);
fail(“An invalid person name should be thrown”);
} catch (InvalidPersonName ipn) {
// Exception expected
}
}
…
Writing a Unit Test
apps
..
ctb }
…
test
oracle public class SomeClassTest
{
apps public void testSomeMethod()
{
ctb ..
… }
}
Running Unit Tests
Define standard Ant targets
Run unit tests automatically and
continuously
Implement code coverage tools
Line not
executed
Number of times
executed
Executed line
Quality of Unit Tests
Mock Objects
What to Test and How Much to Test
– Bugs
– New Functionality
Optimize Running Time
Code Coverage
Environment Management
– Continuous Integration
– Local and remote development
Conclusion
Unit testing adds enormous value to
software development
JUnit makes testing java programs easy
Advanced Unit Testing Concepts
Questions?