Your SlideShare is downloading. ×
0
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Junit Recipes  - Elementary tests (1/2)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Junit Recipes - Elementary tests (1/2)

168

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
168
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. JUnit Recipes- Elementary tests Zheng-Wen Shen 2007/10/25 1
  • 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 2
  • 3. The Building Blocks 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 3
  • 4. The Building Blocks 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 4
  • 5. 1. Test your equals methods (1/3) Test the implementation of equals() Value Object: represents a value  String, Integer, Double, Money, Timestamp, etc.Money a= new Money(100,0);Money b= new Money(100,0); aMoney c= new Money(50,0); bMoney d= new Money(50,0); 100, 0 100, 0a.equals( b ); // truea.equals( c ); // falsec.equals( d ); // true cc.equals( a ); // false 50, 0 d 50, 0 5
  • 6. 1. Test your equals methods (2/3) Test the implementation of equals() The contract of equals()  Equivalence relations (RST) • Reflexive property • Symmetric property • Transitive property  Consistent  No object equals null 6
  • 7. 1. Test your equals methods (3/3) Test the implementation of equals() equal to anot equal to a“looks equal” JUnit utility RST, consistent, no object is equal null… 7
  • 8. 2. Test a method that returns nothing (1/3)  “How do I test a method that return void?”  If a method returns no value, it must have some observable side effect!xyz obj = new xyz(); xyz object stateobj.change(); // transit to Aobj.change(); // transit to B Start Aobj.change(); // transit to Cobj.change(); // transit to D B C 8
  • 9. 2. Test a method that returns nothing (2/3)  To test the Collection.add(Object) 1. Create an empty collection 2. The collection should not contain the item in question 3. Add the item in question 4. Now the collection should contain the item in question 1 2return nothing 3 4 9
  • 10. 2. Test a method that returns nothing (3/3)  We are testing behavior, and not methods.  If code does the wrong thing but no test fails, does it have as defect? NOTE The tests are the specification!!  We describe what our code does by providing the tests our code passes. 10
  • 11. 3. Test a constructor Uses exposed internal state Observable side effect Pitfalls 11
  • 12. 4. Test a getter (1/3) Which tests are needed and which are not Do not test methods that too simple to break!! too simple 12
  • 13. 4. Test a getter (2/3) Which tests are needed and which are not Compare that result with an expected value 13
  • 14. 4. Test a getter (3/3) Which tests are needed and which are not An alternative implementation…Too simple to break 14
  • 15. 5. Test a setter (1/3) Should I test my set methods?  Basic set methods are too simple to break  Effective way to test if you have to:pattern 1 2 3 4 1. Name the test method appropriately 2. Create an instance of your bean class 3. If newPropertyValue is a complex property, then initialize newPropertyValue. 4. If property is a more complex object than string, then you need to ensure that equals() is appropriately implemented 15
  • 16. 5. Test a setter (2/3) Should I test my set methods? If there are no get methods…Command Analyze the side effect 16
  • 17. 5. Test a setter (3/3) Should I test my set methods? BankTransferAction action = new BankTransferAction(); action.setSourceAccountId("source"); action.setTargetAccountId("target"); action.setAmount(Money.dollars(100)); Bank“Spy” Subclass Of Bank (Spy) 17
  • 18. The Building Blocks Elementary tests1. Test your equals methods2. Test a method that returns nothing3. Test a constructor4. Test a getter5. Test a setter to be continue…6. 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 18

×