3. Brief contents
Part 1: The Building Blocks
1. Fundamentals
2. Elementary tests
3. Organizing and building JUnit tests
4. Managing test suites
5. Working with test data
6. Running JUnit tests
7. Reporting JUnit results
8. Troubleshooting JUnit
Part 2: Testing J2EE
Part 3: More JUnit Techniques
3
4. Brief contents
Part 1: The Building Blocks
1. Fundamentals
2. Elementary tests
3. Organizing and building JUnit tests
4. Managing test suites
5. Working with test data
6. Running JUnit tests
7. Reporting JUnit results
8. Troubleshooting JUnit
Part 2: Testing J2EE
Part 3: More JUnit Techniques
4
8. Fundamentals 4/5
The rhythm of an Object Test
1. Create an object
2. Invoke a method
3. Check the result
Application
Obj Obj Obj
Obj Obj Obj
Object testing
Obj Obj Obj Obj
8
9. Fundamentals 5/5
A framework for unit testing
Tests must be automated
Tests must verify themselves
Tests must be easy to run simultaneously
JUnit (CppUnit, xUnit, etc)
Writing automated, self-verifying tests in java
9
11. Enter JUnit 2/5
A Case Study
public class Money {
private int fAmount;
private String fCurrency;
public Money(int amount, String currency) {
fAmount = amount;
fCurrency = currency;
}
public int amount() {
return fAmount;
}
public String currency() {
return fCurrency;
}
public Money add(Money m) {
return new Money(amount()+m.amount(), currency());
}
}
11
12. Enter JUnit 3/5
How to Write A TestCase
public class MoneyTest extends TestCase {
public void testSimpleAdd() {
Money m12CHF= new Money(12, "CHF"); // (1)
Money m14CHF= new Money(14, "CHF");
Money expected= new Money(26, "CHF");
Money result= m12CHF.add(m14CHF); // (2)
assertTrue(expected.equals(result)); // (3)
}
}
(1) Creates the objects we will interact with during the test. This
testing context is commonly referred to as a test's fixture. All we
need for the testSimpleAdd test are some Money objects.
(2) Exercises the objects in the fixture.
(3) Verifies the result
12
14. Enter JUnit 5/5
Structure of Writing A Test
public class MoneyTest extends TestCase {
private Money f12CHF;
private Money f14CHF;
protected void setUp() {
f12CHF= new Money(12, "CHF");
f14CHF= new Money(14, "CHF");
}
protected void tearDown() {
f12CHF= null;
f14CHF= null;
}
public void testSimpleAdd() {
Money expected= new Money(26, "CHF");
Money result= f12CHF.add(f14CHF);
Assert.assertTrue(expected.equals(result));
}
}
14
15. Test-Driven Development (TDD)
Your system is entirely covered by
tests
You build your system from loosely
coupled, highly cohesive objects.
You make steady progress, improving
the system incrementally by making
one test pass, then another, then
another, and so on.
A passing test is never more than a
few minutes away, giving you
confidence and continual positive
feedback.
15
16. Brief contents
Part 1: The Building Blocks
1. Fundamentals
2. Elementary tests
3. Organizing and building JUnit tests
4. Managing test suites
5. Working with test data
6. Running JUnit tests
7. Reporting JUnit results
8. Troubleshooting JUnit
Part 2: Testing J2EE
Part 3: More JUnit Techniques
16
17. Elementary tests
1. Test your equals methods
2. Test a method that returns nothing
3. Test a constructor
4. Test a getter
5. Test a setter
6. Test an interface
7. Test throwing the right exception
8. Let collections compare themselves
9. Test a big object for equality
10.Test an object that instantiates other objects
17