Testing untestable code Stephan Hochdörfer, bitExpert AG
Testing untestable code About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  S.Hochdoerfer@bit...
Testing untestable code No excuse for writing bad code!
Testing untestable code Not to freak out!
Testing untestable code Creativity matters!
Testing untestable code     "There is no secret to writing tests,       there are only secrets to write               test...
Testing untestable code What is „untestable code“?
Testing untestable code Wrong object construction         “new” is evil!                          s
Testing untestable code Tight coupling
Testing untestable code Uncertainty
Testing untestable code       "...our test strategy requires us to       have more control [...] of the sut."      Gerard ...
Testing untestable code In a perfect world...                  Unittest                   Unittest   SUT                  ...
Testing untestable code Legacy code is not perfect...                                 Dependency                          ...
Testing untestable code                                 ... Legacy code is not perfect...                                 ...
Testing untestable code                                 ... Legacy code is not perfect...                                 ...
Testing untestable code How to get „testable“ code?
Testing untestable code How to get „testable“ code?                   Refactoring
Testing untestable code     "Before you start refactoring, check     that you have a solid suite of tests."               ...
Testing untestable code Hope? - Nope...
Testing untestable code Which path to take?
Testing untestable code Which path to take?       Do not change existing code!
Testing untestable code Examples Object construction      External resources   Language issues
Testing untestable code Object construction public class Car {    private Engine engine;     public Car(String engine) {  ...
Testing untestable code Object construction - Solution public class MyLoader extends ClassLoader {    protected Class find...
Testing untestable code Object construction - Solution II <?php class MyStreamWrapper {   private $_handler;     function ...
Testing untestable code External resources
Testing untestable code External resources             Database     Webservice            Filesystem    Mailserver
Testing untestable code External resources – Mock database
Testing untestable code External resources – Mock database               Provide own db driver
Testing untestable code External resources – Mock database                      junit + dbunit
Testing untestable code External resources – Mock database            Proxy for your SQL Server
Testing untestable code External resources – Mock webservice
Testing untestable code External resources – Mock webservice          Provide own implementation
Testing untestable code External resources – Mock webservice                     Do not forget!           Url redirection ...
Testing untestable code External resources – Mock filesystem
Testing untestable code External resources – Mock filesystem                    Commons-VFS                          vfsSt...
Testing untestable code External resources – Mock filesystem <?php // set up test environmemt vfsStream::setup(exampleDir)...
Testing untestable code External resources – Mock Mailserver
Testing untestable code External resources – Mock Mailserver                Use fake mail server
Testing untestable code Dealing with language issues
Testing untestable code Dealing with language issues             Testing your privates?
Testing untestable code Dealing with language issues final Method methods[] = o.getClass().getDeclaredMethods(); for (int ...
Testing untestable code Dealing with language issues       Overwrite internal functions?
Testing untestable code What else?
Testing untestable code What else?           Generative Programming
Testing untestable code Generative Programming                 Configuration                  Configuration               ...
Testing untestable code Generative Programming                 Configuration                  Configuration               ...
Testing untestable code Generative Programming        A frame is a data structure       for representing knowledge.
Testing untestable code A Frame instance public class Car {    private Engine engine;     public Car(String engine) {     ...
Testing untestable code The Frame controller public class MyFrameController extends SimpleFrameController {    public void...
Testing untestable code Generated result - Testcase public class Car {    private Engine engine;     public Car(String eng...
Testing untestable code Generated result - Application public class Car {    private Engine engine;     public Car(String ...
Testing untestable code What is possible?
Testing untestable code What is possible?          Show / hide parts of the code
Testing untestable code What is possible?         Change content of global vars!
Testing untestable code What is possible?              Define pre- or postfixes!
Testing untestable code Is it worth it?
Thank you for attending this session. Please fill out an evaluation form.
Upcoming SlideShare
Loading in...5
×

Testing untestable code - STPCon11

989

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
989
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
12
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Testing untestable code - STPCon11

  1. 1. Testing untestable code Stephan Hochdörfer, bitExpert AG
  2. 2. Testing untestable code About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  S.Hochdoerfer@bitExpert.de  @shochdoerfer
  3. 3. Testing untestable code No excuse for writing bad code!
  4. 4. Testing untestable code Not to freak out!
  5. 5. Testing untestable code Creativity matters!
  6. 6. Testing untestable code "There is no secret to writing tests, there are only secrets to write testable code!" Miško Hevery
  7. 7. Testing untestable code What is „untestable code“?
  8. 8. Testing untestable code Wrong object construction “new” is evil! s
  9. 9. Testing untestable code Tight coupling
  10. 10. Testing untestable code Uncertainty
  11. 11. Testing untestable code "...our test strategy requires us to have more control [...] of the sut." Gerard Meszaros, xUnit Test Patterns: Refactoring Test Code
  12. 12. Testing untestable code In a perfect world... Unittest Unittest SUT SUT
  13. 13. Testing untestable code Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency
  14. 14. Testing untestable code ... Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency ...
  15. 15. Testing untestable code ... Legacy code is not perfect... Dependency Dependency Unittest Unittest SUT SUT Dependency Dependency ...
  16. 16. Testing untestable code How to get „testable“ code?
  17. 17. Testing untestable code How to get „testable“ code? Refactoring
  18. 18. Testing untestable code "Before you start refactoring, check that you have a solid suite of tests." Martin Fowler, Refactoring
  19. 19. Testing untestable code Hope? - Nope...
  20. 20. Testing untestable code Which path to take?
  21. 21. Testing untestable code Which path to take? Do not change existing code!
  22. 22. Testing untestable code Examples Object construction External resources Language issues
  23. 23. Testing untestable code Object construction public class Car { private Engine engine; public Car(String engine) { this.engine = EngineFactory.getByType(engine); } }
  24. 24. Testing untestable code Object construction - Solution public class MyLoader extends ClassLoader { protected Class findClass(String name) throws ClassNotFoundException { try { String path = name.replace(., /); // @TODO: change path and read file, // @TODO: define package // @TODO: define class } catch(Exception e){ throw new ClassNotFoundException(name); } } }
  25. 25. Testing untestable code Object construction - Solution II <?php class MyStreamWrapper { private $_handler; function stream_open($path, $mode, $options, &$opened_path) { stream_wrapper_restore(file); // @TODO: modify $path before fopen $this->_handler = fopen($path, $mode); stream_wrapper_unregister(file); stream_wrapper_register(file, MyStreamWrapper); return true; } }
  26. 26. Testing untestable code External resources
  27. 27. Testing untestable code External resources Database Webservice Filesystem Mailserver
  28. 28. Testing untestable code External resources – Mock database
  29. 29. Testing untestable code External resources – Mock database Provide own db driver
  30. 30. Testing untestable code External resources – Mock database junit + dbunit
  31. 31. Testing untestable code External resources – Mock database Proxy for your SQL Server
  32. 32. Testing untestable code External resources – Mock webservice
  33. 33. Testing untestable code External resources – Mock webservice Provide own implementation
  34. 34. Testing untestable code External resources – Mock webservice Do not forget! Url redirection via /etc/hosts
  35. 35. Testing untestable code External resources – Mock filesystem
  36. 36. Testing untestable code External resources – Mock filesystem Commons-VFS vfsStream
  37. 37. Testing untestable code External resources – Mock filesystem <?php // set up test environmemt vfsStream::setup(exampleDir); // create directory in test enviroment mkdir(vfsStream::url(exampleDir)./sample/); // check if directory was created echo vfsStreamWrapper::getRoot()->hasChild(sample);
  38. 38. Testing untestable code External resources – Mock Mailserver
  39. 39. Testing untestable code External resources – Mock Mailserver Use fake mail server
  40. 40. Testing untestable code Dealing with language issues
  41. 41. Testing untestable code Dealing with language issues Testing your privates?
  42. 42. Testing untestable code Dealing with language issues final Method methods[] = o.getClass().getDeclaredMethods(); for (int i = 0; i < methods.length; ++i) { if (methodName.equals(methods[i].getName())) { try { methods[i].setAccessible(true); return methods[i].invoke(o, params); } catch (IllegalAccessException ex) { } catch (InvocationTargetException ite) { } } return null; }
  43. 43. Testing untestable code Dealing with language issues Overwrite internal functions?
  44. 44. Testing untestable code What else?
  45. 45. Testing untestable code What else? Generative Programming
  46. 46. Testing untestable code Generative Programming Configuration Configuration 1 ... n Implementation Implementation Generator Generator Product components components Product Generator Generator application application
  47. 47. Testing untestable code Generative Programming Configuration Configuration Application Application Implementation Implementation Generator Generator components components Testcases Testcases Generator Generator application application
  48. 48. Testing untestable code Generative Programming A frame is a data structure for representing knowledge.
  49. 49. Testing untestable code A Frame instance public class Car { private Engine engine; public Car(String engine) { this.engine = <!{Factory}!>.getByType(engine); } }
  50. 50. Testing untestable code The Frame controller public class MyFrameController extends SimpleFrameController { public void execute(Frame frame, FeatureConfig config) { if(config.hasFeature("unittest")) { frame.setSlot("Factory", "FactoryMock"); } else { frame.setSlot("Factory", "EngineFactory"); } } }
  51. 51. Testing untestable code Generated result - Testcase public class Car { private Engine engine; public Car(String engine) { this.engine = FactoryMock.getByType(engine); } }
  52. 52. Testing untestable code Generated result - Application public class Car { private Engine engine; public Car(String engine) { this.engine = EngineFactory.getByType(engine); } }
  53. 53. Testing untestable code What is possible?
  54. 54. Testing untestable code What is possible? Show / hide parts of the code
  55. 55. Testing untestable code What is possible? Change content of global vars!
  56. 56. Testing untestable code What is possible? Define pre- or postfixes!
  57. 57. Testing untestable code Is it worth it?
  58. 58. Thank you for attending this session. Please fill out an evaluation form.
  1. A particular slide catching your eye?

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

×