Transcript JUnit_Intro
An Introduction to JUnit
Greg Jackson
June 2004
Contact Info
• Northrop Grumman Corp.
• [email protected]
• 904.825.6162 (work)
Software Quality Assurance & Testing
2
Why Test?
• Testing is an important part of large
enterprise systems.
• Humans make mistakes
• Bugs cost time & money
Software Quality Assurance & Testing
3
Why Automated Testing?
• Humans hate testing.
• Testing will show when your code started
working and when it breaks.
• Automated unit-level testing is an essential
part of continuous integration.
• Instantly verify that re-factoring didn’t break
code.
• Allows scheduled verification of code.
• Documented test results.
• Test often.
Software Quality Assurance & Testing
4
Automated Unit Test
• Unit tests help you to make sure that what is
documented in the API of your classes is
actually what you have implemented.
• Unit test can help you write your code or
define what you want your code to do.
• They test for success, failures, and errors.
Software Quality Assurance & Testing
5
Cost
• Automated testing takes more time to develop
then performing manual testing.
• ROI occurs during system maintenance.
Software Quality Assurance & Testing
6
Java
• Why learn how to test a Java application?
Software Quality Assurance & Testing
7
Companies Using Java
• Presence in Jacksonville
• Citigroup (8)
• Bank of America (24)
• Northrop Grumman (55)
• *Winn Dixie (162)
• *CSX (231)
• *Fidelity Information Services (262)
• Landstar
*Headquarters in Jacksonville
(2003 Fortune 500 Ranking)
Software Quality Assurance & Testing
8
Writing Testing Code
• Debugger
Simple
Change debug expressions without
recompiling.
• Print to the standard output stream
System.out.println()
• Problems:
Require human judgment to analyze
results.
Only execute on at a time.
Scroll blindness.
Software Quality Assurance & Testing
9
Writing Testing Code
• Solution: Automated Testing
Don’t require human judgment to interpret.
Easy to run many at the same time.
Software Quality Assurance & Testing
10
JUnit Framework
• JUnit is a framework for writing unit
test for Java classes.
• Written by Erich Gamma and Kent Beck.
• Open Source
http://www.junit.org
http://sourceforge.net/projects/junit/
Software Quality Assurance & Testing
11
Definitions
• Test Case – defines a fixture to run a
related set of test. Typically every class
you write should have a test case.
• Test Fixture – provides resources:
primitive variables and objects that test
need to run.
• Test Suite – a collection of related test
cases.
Software Quality Assurance & Testing
12
Stack Example
•
•
•
•
Push
Pop
Top
Is Empty
Software Quality Assurance & Testing
13
Steps to Writing a Test Case
1. Subclass junit.framework.TestCase
2. Override the setUp() method to create fixture
objects.
3. Define a number of test that return void and
whose method name begins with test, such as
testAdd(), testPut(), and testIterator().
4. Override the tearDown() method to release
resources that were part of fixture.
5. Group releated sets of test cases by defining
a suite of tests.
Software Quality Assurance & Testing
14
JUnit TestCase Methods
public class StackTest extends TestCase {
public StackTest(String arg0) {…}
protected void setUp() {…}
protected void tearDown() {…}
public void testPush() {…}
public static Test suite() {…}
public static void main(String[] args) {…}
}
Software Quality Assurance & Testing
15
JUnit TestCase Class
• Define instance variables that store the
state of the fixture.
• Initialize the fixture state by overriding
setUp.
• Clean-up after a test by overriding
tearDown
• Each test runs its own fixture so there
can be no side effects among test runs.
Software Quality Assurance & Testing
16
MathTest
public class MathTest extends TestCase{
protected double x;
protected double y;
}
Software Quality Assurance & Testing
17
MathTest.setUp()
public class MathTest extends TestCase{
protected double x;
protected double y;
protected void setUp() {
x = 2.0;
y = 3.0;
}
}
Software Quality Assurance & Testing
18
tearDown()
public class MathTest extends TestCase{
protected double x;
protected double y;
protected void tearDown() {
\\ simple test
}
}
Software Quality Assurance & Testing
19
MathTest.testAdd()
public class MathTest extends TestCase{
public void testAddXY() {
double result = x + y;
assertTrue(result == 5.0);
}
public void testAddXX() {
double result = x + x;
assertTrue(result == 4.0);
}
}
Software Quality Assurance & Testing
20
Running the MathTest
TestCase test = new MathTest(“testAddXY”);
test.run();
TestCase test2 = new MathTest(“testAddXX”);
test2.run();
Software Quality Assurance & Testing
21
JUnit TestSuite Class
• The test to run can be collected into a TestSuite.
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new MathTest(“testAddXY”));
suite.addTest(new MathTest(“testAddXX”));
return suite;
}
public static Test suite() {
return new TestSuite(MathTest.class);
}
Software Quality Assurance & Testing
22
JUnit TestRunner Class
• TestRunner expects the name of a TestCase
class as argument. If the class defines a static
suite method it will be invoked and the
returned test is run. Otherwise all the
methods starting with “test” having no
arguments are run.
• junit.textui.TestRunner
• junit.awtui.TestRunner
• junit.swingui.TestRunner
public static void main(String[] args) {
TestRunner.run(MathTest.class);
}
Software Quality Assurance & Testing
23
assertEquals()
assertEquals(boolean expected, boolean actual)
– Asserts that two booleans are equal.
– Only displays a message when an assert fails.
Supports: boolean, byte, char, double, float, int, long,
Object, short, String.
assertEquals(String message, boolean expected,
boolean actual)
– Uses supplied message when assert fails.
Software Quality Assurance & Testing
24
assertFalse() / assertTrue()
assertFalse(boolean condition)
assertFalse(String message, boolean
condition)
- Asserts that a condition is false.
assertTrue(boolean condition)
assertTrue(String message, boolean
condition)
- Asserts that a condition is true.
Software Quality Assurance & Testing
25
assertNotNull() / assertNull()
assertNotNull(java.lang.Object object)
- Asserts that an object isn’t null.
assertNull(java.lang.Object object)
- Asserts that an object is null.
Software Quality Assurance & Testing
26
assertSame() / assertNotSame()
assertSame(java.lang.Object expected,
java.lang.Object actual)
- Asserts that two objects refer to the same
object.
assertNotSame(java.lang.Object expected,
java.lang.Object actual)
- Asserts that two objects do not refer to the
same object.
Software Quality Assurance & Testing
27
fail()
fail()
- Fails a test with no message.
fail(java.lang.String message)
- Fails a test with the given message.
Software Quality Assurance & Testing
28
Testing for Exceptions
try {
int x = 2 / 0;
fail(“Should have thrown ArithmeticException”);
}
catch(ArithmeticException e){
// test passed
}
Software Quality Assurance & Testing
29
Pitfalls
•
•
•
•
•
•
No assert
Unreasonable assert
Console-Based Testing
Unfocused Test Method
Failure to Isolate Each Test
Failure to Isolate Subject
Software Quality Assurance & Testing
30
No Assert
• Problem:
• Assume that invoking a method is a
sufficient test.
• Assume that if not exceptions are thrown,
everything must be OK.
• Solution:
• Introduce asserts.
• The intent of the API needs to be asserted
to make sure the documented behavior is
actually happening.
Software Quality Assurance & Testing
31
Unreasonable assert
• Problem:
• Tendency to assert everything that can be
imagined.
• Asserting that the JVM is working.
• Solution:
• The intent of the API needs to be asserted
to make sure the documented behavior is
actually happening.
• Ask “What aspect of the API does this
assert test?”, if no clear answer remove the
assert.
Software Quality Assurance & Testing
32
Console-Based Testing
• Problem:
• Use of System.out.println
• Solution:
• System.out becomes assert
• Why is data printed?
• If it is to see what is there, often an
assertNotNull can take its place.
• Visual comparison of values are
converted to assertions.
Software Quality Assurance & Testing
33
Unfocused Test Method
• Problem:
• Test method unfocused.
• Difficult to determine what is being tested.
• Not using setUp and tearDown, instead put
all code in test method.
• Solution:
• Keep is simple.
• Place each related group of asserts into a
separate test method.
• Decompose the complex test.
• As a general rule, each test method should
have one assert.
Software Quality Assurance & Testing
34
Failure to Isolate Each Test
• Problem:
• Test requires external script to run before
test will succeed (ie: initialization of data)
• Order dependencies (ie: one test creates an
object, a second test updates it, and a third
test deletes the object)
• Not cleaning up database after test run.
• Solution:
• Use the setUp and tearDown methods.
• Test decorators perform larger setup only
once (less resources). They extend
TestDecorator or TestSetup. Take care of
create and remove calls.
Software Quality Assurance & Testing
35
Failure to Isolate Subject
• Problem:
• Failure to isolate test subject from other
classes they rely on.
• Subject test failure caused by code other
than the test subject.
• Solution:
• Introduce mock objects to replace the
objects that the test subject depends on.
• Mock objects provide the same API but yield
hard-coded data and simplified
functionality.
Software Quality Assurance & Testing
36
Questions on JUnit
?
Software Quality Assurance & Testing
37
Cactus
• Cactus is a simple test framework for unit
testing server-side java code (Servlets, EJBs,
Tag Ligs, Filters, …)
• Extends JUnit.
• Supports Mock Object or Container Testing.
• http://jakarta.apache.org/cactus
Software Quality Assurance & Testing
38
HttpUnit
• Allows testing of a web application.
• Emulates the relevant portions of browser
behavior, including submission, JavaScript,
basic http authentications, cookies, and
automatic page redirections, allowing Java test
code to examine returned pages either as test,
an XML DOM, or containers of forms, tables,
and links.
• Combine with JUnit to write test to verify
functionality of a web site.
Software Quality Assurance & Testing
39
Questions
?
Software Quality Assurance & Testing
40
References
Jakarta Pitfalls: Time-Saving Solutions for
Struts, Ant, JUnit, and Cactus (Java Open
Source Library)
Bill Dudney, Jonathan Lehr
ISBN: 0-471-44915-6
Java Tools for Extreme Programming:
Mastering Open Source Tools, Including Ant,
JUnit, and Cactus
Richard Hightower, Nicholas Lesiecki
ISBN: 0-471-20708-X
Software Quality Assurance & Testing
41