100% Confident with Legacy
Code
Johan MARTINSSON
Developer - Independent
martinsson.johan@gmail.co
m
@johan_alps
Rémy SANL...
Working with Legacy Code
Working with Legacy Code
Refactoring Legacy Code with Object
Calisthenics2012 - 2013
Anonymous Developers - Season 1
Working with Legacy Code
100% Confident with Legacy Code
2014
Anonymous Developers - Season 2
Testing Legacy Code - Blockers
Integration tests are too long
Unit tests are too coupled to the source code
Hard and long ...
Testing Legacy Code
100% Confident with Legacy Code
2014
Anonymous Developers - Season 2
Testing Legacy Code
100% Confident with Legacy Code
2014
Anonymous Developers - Season 2
Testing Legacy Code
Don’t forget to replace
temporary refactoring
tests
Trivia Challenge http://www.hasbro.com/games/en_US/shop/details.cfm?R=93C6EE71-
6D40-1014-8BF0-9EFBF894F9D4:en_US
https://...
Trivia Challenge
https://github.com/jbrains/trivia
http://www.hasbro.com/games/en_US/shop/details.cfm?R=93C6EE71-
6D40-101...
Server 1
Server 2
EncodeAudioLegacy Challenge
Remote Server 1
Audio File encoding
HTTPS Server 2
Audio File Encoded
Audio ...
Baby Steps
Step0: Development environment validation
Step1: String comparison with ApprovalTests
Step2: Branch coverage by...
Step 0: Development environment validation
EclEmmaJUnit
Technical Solution: Unit Testing + Code Coverage Tool + Diff
Tool
...
Step 1: Method returns a primitive type
Technical Solution: Approval Tests (string comparison)
Context: No side effect + M...
Step 2: Branches coverage by input
parameters variation
Technical Solution: Approval Tests (legacyApprovals)
Context: No s...
Step 3: Method returns a complex type
Note: date issues
Context: No side effect + Method returns a complex type
Technical ...
Server 2
encoding
HTTPS Server 2
Audio File available
at Remote Server 1
Encode Audio Engine
upload
Audio File Encoded
Aud...
Step 5: Capturing side effects
Note: can be used for context with void methods
not mocking side effect
Context: Side effec...
Conclusion - Why it’s working?
Easy assertion: record and replay
Even ugly tests do the job (Temporary tests)
Fast to exec...
Next…
http://approvaltests.sourceforge.net/
http://martinsson-johan.blogspot.fr/
http://github.com/dreamhead/moco
https://...
http://approvaltests.sourceforge.net/
http://approvaltests.sourceforge.net/
Approval Tests
only needs 2
lines!
http://approvaltests.sourceforge.net/
Run your test
Diff Tool (by default TortoiseMerge)
Cf. Using Reporters in Approval T...
http://approvaltests.sourceforge.net/
Result of the current test
named received
http://approvaltests.sourceforge.net/
Result reference
named approved
http://approvaltests.sourceforge.net/
Approve result
and save file
http://approvaltests.sourceforge.net/
BACKUP
Server 1
Server 2
EncodeAudioLegacy Challenge
Remote Server 1
Audio File encoding
HTTPS Server 2
Audio File available
at R...
100 % Confident With Legacy Code
Upcoming SlideShare
Loading in...5
×

100 % Confident With Legacy Code

2,324

Published on

I T.A.K.E 2014 unconference

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

  • Be the first to like this

No Downloads
Views
Total Views
2,324
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

100 % Confident With Legacy Code

  1. 1. 100% Confident with Legacy Code Johan MARTINSSON Developer - Independent martinsson.johan@gmail.co m @johan_alps Rémy SANLAVILLE Developer - Orange Software Expert remy.sanlaville@orange.com @sanlaville
  2. 2. Working with Legacy Code
  3. 3. Working with Legacy Code Refactoring Legacy Code with Object Calisthenics2012 - 2013 Anonymous Developers - Season 1
  4. 4. Working with Legacy Code 100% Confident with Legacy Code 2014 Anonymous Developers - Season 2
  5. 5. Testing Legacy Code - Blockers Integration tests are too long Unit tests are too coupled to the source code Hard and long to write tests
  6. 6. Testing Legacy Code 100% Confident with Legacy Code 2014 Anonymous Developers - Season 2
  7. 7. Testing Legacy Code 100% Confident with Legacy Code 2014 Anonymous Developers - Season 2
  8. 8. Testing Legacy Code Don’t forget to replace temporary refactoring tests
  9. 9. Trivia Challenge http://www.hasbro.com/games/en_US/shop/details.cfm?R=93C6EE71- 6D40-1014-8BF0-9EFBF894F9D4:en_US https://github.com/jbrains/trivia It simulates a trivia game using a randomizer for correct and wrong answers How long do you need to reach ~100% code coverage ?
  10. 10. Trivia Challenge https://github.com/jbrains/trivia http://www.hasbro.com/games/en_US/shop/details.cfm?R=93C6EE71- 6D40-1014-8BF0-9EFBF894F9D4:en_US It works well because there are no dependencies 1 1 Serialize game state and compare it with Approval Tests 1 Cover all branches by variying the results of the randomizer 2 2
  11. 11. Server 1 Server 2 EncodeAudioLegacy Challenge Remote Server 1 Audio File encoding HTTPS Server 2 Audio File Encoded Audio File Audio File available at Remote Server 1 Encoded Audio File available download upload Encode Audio Engine
  12. 12. Baby Steps Step0: Development environment validation Step1: String comparison with ApprovalTests Step2: Branch coverage by input parameter variation Step3: Serializing a complex type Step4: Mocking web services Step5: Capturing side effects
  13. 13. Step 0: Development environment validation EclEmmaJUnit Technical Solution: Unit Testing + Code Coverage Tool + Diff Tool Context: Launch UT + Code Coverage + String comparison Server 1 Remote Server 1 Audio File download Server 2 encoding HTTPS Server 2 Audio File available at Remote Server 1 Encode Audio Engine upload Audio File Encoded Audio File Encoded Audio File Encoded Audio File available Meld WinMerge…
  14. 14. Step 1: Method returns a primitive type Technical Solution: Approval Tests (string comparison) Context: No side effect + Method returns a primitive type Server 1 Remote Server 1 Audio File download Server 2 encoding HTTPS Server 2 Audio File available at Remote Server 1 Encode Audio Engine upload Audio File Encoded Audio File Encoded Audio File Encoded Audio File available Note: encoding issues
  15. 15. Step 2: Branches coverage by input parameters variation Technical Solution: Approval Tests (legacyApprovals) Context: No side effect + Method returns a primitive type Server 2 encoding HTTPS Server 2 Audio File available at Remote Server 1 Encode Audio Engine upload Audio File Encoded Audio File Encoded Audio File Encoded Audio File available Server 1 Remote Server 1 Audio File download
  16. 16. Step 3: Method returns a complex type Note: date issues Context: No side effect + Method returns a complex type Technical Solution: Approval Tests + XStream Server 1 Remote Server 1 Audio File download Server 2 encoding HTTPS Server 2 Audio File available at Remote Server 1 Encode Audio Engine upload Audio File Encoded Audio File Encoded Audio File Encoded Audio File available IFluxTmlg IBodyTmlg ITravelInfoTmlg IAudioAnnounceTmlg
  17. 17. Server 2 encoding HTTPS Server 2 Audio File available at Remote Server 1 Encode Audio Engine upload Audio File Encoded Audio File Encoded Audio File Encoded Audio File available Step 4: Third-party services Context: WebService difficult to configure or/and not avalaible Technical Solution: Moco Server 1 Remote Server 1 Audio File download
  18. 18. Step 5: Capturing side effects Note: can be used for context with void methods not mocking side effect Context: Side effects Technical Solution: State, Side Effects Serialization + Approval Tests Server 1 Remote Server 1 Audio File download Server 2 encoding HTTPS Server 2 Audio File available at Remote Server 1 Encode Audio Engine upload Audio File Encoded Audio File Encoded Audio File Encoded Audio File available
  19. 19. Conclusion - Why it’s working? Easy assertion: record and replay Even ugly tests do the job (Temporary tests) Fast to execute Available good libraries: Approval Tests, XStream, Moco… Tests are resilient to refactoring
  20. 20. Next… http://approvaltests.sourceforge.net/ http://martinsson-johan.blogspot.fr/ http://github.com/dreamhead/moco https://github.com/pearlfish/pearlfish-java UI visual diffing https://www.youtube.com/watch?v=UMnZiTL0tUc Working Effectively with Legacy Code – in particular for creating “seams”
  21. 21. http://approvaltests.sourceforge.net/
  22. 22. http://approvaltests.sourceforge.net/ Approval Tests only needs 2 lines!
  23. 23. http://approvaltests.sourceforge.net/ Run your test Diff Tool (by default TortoiseMerge) Cf. Using Reporters in Approval Tests http://blog.approvaltests.com/2011/12/using-reporters-in-approval-tests.html
  24. 24. http://approvaltests.sourceforge.net/ Result of the current test named received
  25. 25. http://approvaltests.sourceforge.net/ Result reference named approved
  26. 26. http://approvaltests.sourceforge.net/ Approve result and save file
  27. 27. http://approvaltests.sourceforge.net/
  28. 28. BACKUP
  29. 29. Server 1 Server 2 EncodeAudioLegacy Challenge Remote Server 1 Audio File encoding HTTPS Server 2 Audio File available at Remote Server 1 download Encode Audio Engine upload AudioAnnouceTmlg URL Remote Server 1 format filename audioConfigTmp target audio file extension target encoding properties httpConfigTmp Local Tempory Folder path, URL Remote HTTPS Server 2 Local Tempory Folder Audio File Encoded Audio File Local HTTP Server Encoded Audio File Encoded Audio File available AudioFile name format
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×