0% found this document useful (0 votes)
3 views21 pages

Unit Testing

The document discusses the importance and benefits of unit testing compared to traditional testing methods, highlighting faster debugging, better design, and cost reduction. It introduces JUnit as a mature, Java-based unit testing framework, outlining its key concepts, best practices, and advanced strategies such as mock objects. The conclusion emphasizes the significant value that unit testing brings to software development.

Uploaded by

yoceban962
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

Unit Testing

The document discusses the importance and benefits of unit testing compared to traditional testing methods, highlighting faster debugging, better design, and cost reduction. It introduces JUnit as a mature, Java-based unit testing framework, outlining its key concepts, best practices, and advanced strategies such as mock objects. The conclusion emphasizes the significant value that unit testing brings to software development.

Uploaded by

yoceban962
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit Testing

[email protected]
Australian Development Centre
Brisbane, Australia
Aims

 Unit Testing vs Traditional Testing


 Benefits of Unit Testing
 Introduction to xUnit (using JUnit)
frameworks
 Advanced Unit Testing Strategies
Traditional Testing

 Test the system as a


whole
 Individual
components rarely
tested
 Errors go undetected
 Isolation of errors
difficult to track
down
Traditional Testing
Strategies
 Print Statements
 Use of Debugger
 Debugger Expressions
 Test Scripts
Unit Testing

 Each part tested


individually
 All components
tested at least once
 Errors picked up
earlier
 Scope is smaller,
easier to fix errors
Unit Testing Ideals

 Isolatable
 Repeatable
 Automatable
 Easy to Write
Why Unit Test?

 Faster Debugging
 Faster Development
 Better Design
 Excellent Regression Tool
 Reduce Future Cost
Unit Tests

 Simple Standalone Classes


 High Level Classes
 Database based Classes
 Integrated Framework Classes
JUnit (www.junit.org)

 Java-based unit  Mature Framework


testing framework  De facto java
 Elegantly simple standard
 Easy to write unit  Ant integration
tests  Generic testing
 Easy to manage unit framework
tests
 Open source = Free!
JUnit Is Not Perfect

 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

1. Create a class to hold the unit tests


2. Initialise objects (setUp() method)
3. (State assertions – preconditions)*
4. Call operations on the objects that are
being unit tested
5. State assertions/failures expected
6. Clean up (tearDown() method)
7. Execute the unit test
JUnit Best Practices

 Setting up unit tests


 Running unit tests
 Writing unit tests
Setting up Unit Tests
public class SomeClass
{
..
ctb
public void someMethod()
src {
oracle }
..

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

 Number of Unit Tests


 Code Coverage
Writing Unit Tests

 Avoid setup in constructor


 Define tests correctly
 Minimise side-effects of
unit tests
 Leverage Junit’s assertions
and failures to their fullest
 Keep tests small and fast
 Automate all processes
 Write effective exception
handling code
 Add a test case for every
bug exposed
 Refactor, refactor, refactor
Advanced Unit Testing

 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?

You might also like