• Like
  • Save
Junit Recipes - Intro
Upcoming SlideShare
Loading in...5
×
 

Junit Recipes - Intro

on

  • 686 views

 

Statistics

Views

Total Views
686
Views on SlideShare
686
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Junit Recipes - Intro Junit Recipes - Intro Presentation Transcript

    • JUnit Recipes Zheng-Wen Shen 2007/10/11 1
    • References 2
    • 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
    • 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
    • Fundamentals 1/5 We hate debugging !!  Code and Fix phases 5
    • Fundamentals 2/5 6
    • Fundamentals 3/5 We’d rather be Programmer Testing (Unit Testing) !!  Object Testing: testing object in isolation Application Manual testing Obj Obj Obj Obj Obj Obj Object testing Obj Obj Obj Obj 7
    • 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
    • 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
    • Enter JUnit 1/5The framework of JUnit 10
    • Enter JUnit 2/5 A Case Studypublic 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
    • 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. Thistesting context is commonly referred to as a tests fixture. All weneed for the testSimpleAdd test are some Money objects.(2) Exercises the objects in the fixture.(3) Verifies the result 12
    • Enter JUnit 4/5making assertions 13
    • Enter JUnit 5/5 Structure of Writing A Testpublic 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
    • 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
    • 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
    • Elementary tests1. Test your equals methods2. Test a method that returns nothing3. Test a constructor4. Test a getter5. Test a setter6. Test an interface7. Test throwing the right exception8. Let collections compare themselves9. Test a big object for equality10.Test an object that instantiates other objects 17