Software Construction
Unit Testing
Christoph Denzler / Martin Kropp
University of Applied Sciences Northwestern Switzerland
Institute for Mobile and Distributed Systems
Learning Target
You
understand and can explain the importance of
testing
can describe the basic concepts of the JUnit
testing tool
can use and configure JUnit
can organize and write simple tests
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 2
Agenda
Introduction into Testing
Unit Testing
Testing with JUnit
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 3
The Why, The What, The How
It has to do with quality!
Your software has to meet customer
requirements
functional and non-functional requirements
How do you ensure that your software does
what it should?
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 4
The Why, The What, The How
It has to do with safety!
You often have to change your existing code
because of new requirements, improvements,
and bug fixes
How do you ensure, that your changed software
still does what it did before?
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 5
The Why, The What, The How
It has to do with sustainability!
You still have to fix bugs
How do you ensure, that a fixed bug never
shows up again? (or you notice it immediately,
at least)
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 6
What is Testing?
Testing is the process of evaluating a particular
product to determine if it meets the customers
requirements.
That means
1. There must be well specified requirements to
test against (expected results)
2. Testing is comparing actual results to expected
results
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 7
What is Testing?
The deviation of an actual result from an
expected result, is called a defect
Testing can only show the presence of a defect
but not the absence of defects
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 8
The First „Bug“
Grace Hopper’s “bug” (moth stuck in a relay on
an early machine)
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 9
Some Famous Software Failures
1991: The Patriot Missile failure, in Dharan, Saudi Arabia, on February 25, 1991
which resulted in 28 deaths, is ultimately attributable to poor handling of rounding
errors.
1991: The sinking of the Sleipner A offshore platform in Gandsfjorden near
Stavanger, Norway, on August 23, 1991, resulted in a loss of nearly one billion
dollars. It was found to be the result of inaccurate finite element analysis.
1996: The explosion of the Ariane 5 rocket just after lift-off on its maiden voyage
off French Guiana, on June 4, 1996, was ultimately the consequence of a simple
overflow.
1999: NASA’s Mars lander, September 1999, crashed due to a units integration
fault—over $50 million US !
2005: Arbeitslosengeld II = ALG-II in Germany:writing 9 digit bank codes left-
aligned in 10 digit fields leads to adding a zero at the end and wrong bank codes.
2010: Year 2010 bug on German bankcards (Jan 4 2010): It showed up to be a
software defect due to incorrect year handling.
More on http://www5.in.tum.de/~huckle/bugse.html
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 10
The Why, The What, The How
You have to show that
The product works as expected
But also its individual parts, like
services
components
classes
methods
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 11
The Why, The What, The How
You can do this manually
Writing test specifications and protocols
Or (much better) automated
Let the system do it for you (at least part of it)
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 12
Testing Dimensions
Level
system
integration
component
unit
Accessibility
stress
white box black box
performance
reliability
usability
robustness
functionality
Target
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 13
The Testing Process
Test Test
Test case Test data
results report
Design test Prepare test Run Compare test
cases data tests results to test data
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 14
Unit Testing
Unit Testing is done
on each module
in isolation
to verify the unit’s behavior
Unit test will
establish some kind of artificial environment
invoke routines in the module under test
check the results against expected values
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 15
Using Test Frameworks
Provides following capabilities
test specification
test implementation
standard way to specify setup and cleanup
method for selecting individual tests or all available
tests
means of analyzing output for expected (or
unexpected) results
standardized form of failure reporting
=> Use JUnit for Java Unit Testing
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 16
JUnit 4 Example
package unittesting;
import java.util.NoSuchElementException;
public class IntArray {
private int[] array;
public IntArray(int[] values) { array = values; }
public int findLargest() throws NoSuchElementException {
int max = Integer.MAX_VALUE;
if (array.length == 0) { throw new NoSuchElementException(); }
for (int num : array) {
if (num > max) {
max = num;
}
}
return max;
}
}
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 17
JUnit4
Uses annotations, static import…
Setup, Cleanup
@Before, @After
@BeforeClass, @AfterClass
Testing
@Test
@Test(expected=MovieMgmtException.class)
@Ignore
Checking
Assert (use "static import")
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 18
Using JUnit Asserts
Use Asserts by
using the prefixed syntax
Assert.assertEquals()
importing statically the Assert class
import static org.unit.Assert.assertEquals
There are several assertions
assertEquals()
assertEquals([String message], expected, actual)
assertEquals([String message], expected, actual,
tolerance)
assertNull(), assertNotNull(), assertSame(), assertNotSame(),
assertTrue(), assertFalse(), fail()
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 19
Organizing Your Test
Separate tests from production code
myProject/trunk
/src/prod
/src/test // as under prod
/src/test/data // for test data
/log // takes all output
Put it into the same packages
myProject/trunk
/src/prod/ch.fhnw.edu.rental.model
/src/test/ch.fhnw.edu.rental.model
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 20
Reflections on Unit Testing
Coding with Confidence
What is Unit Testing?
Why should I bother with Unit Testing?
What do I want to accomplish?
Does it do what I want?
Does it do what I want all the time?
Can I depend on it?
Does it document my intent?
How do I do Unit Testing?
Bad Excuses for not Unit Testing
It takes too much time to write the tests
It takes too long to run the tests
It's not my job to test my code
I don't really know how the code is supposed to behave so I can't test it
But it compiles
I'm being paid to write code, not to write tests
I feel guilty about putting testers and QA staff out of work
My company won't let me run unit tests on the live system
from "Pragmatic Unit Testing"
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 21
And Finally …
Some pragmatic tips
Test Early. Test Often. Test Automatically.
Tip 62 (The Pragmatic Programmer)
Coding Ain't Done 'Til All the Tests Run.
Tip 63 (The Pragmatic Programmer)
UnitTesting (Part1), FS2010 IMVS, Ch. Denzler/M. Kropp 22