1 / 46

Designing Better Software

Designing Better Software. with Mock Objects using Dependency Injection. Who is this guy?. greg.hutchinson@fcc-fac.ca. Jenga Driven Development (JDD). Jenga Driven Development (JDD). Who are you?. What is meant by better software?. Easier To Test.

gordy
Download Presentation

Designing Better Software

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Designing Better Software with Mock Objects using Dependency Injection

  2. Who is this guy? greg.hutchinson@fcc-fac.ca

  3. Jenga Driven Development (JDD)

  4. Jenga Driven Development (JDD)

  5. Who are you?

  6. What is meant by better software?

  7. Easier To Test

  8. 100% Coverage (Or at least as close as possible)

  9. Looser Coupling

  10. Notations

  11. NotationIClassName is Interface public interface  ICustomerRepository { ... }

  12. Notation  ClassName is Implementation public class CustomerRepository { ... }

  13. Client – Refers to a Class that invokes behaviour in another Class Notation Class X (Client) object.doSomething() Class Y public void doSomething() {

  14. Testing

  15. Tenents of Unit Testing

  16. Tenents of Unit Testing

  17. Tenents of Unit Testing

  18. Dependency Injection

  19. What Problem are we solving?

  20. Remove Dependency from X to Abc, Def 

  21. Dependency Injection

  22. Dependency Injection

  23. Remove Clutter Other Patterns Introduce public class X {     private IAbc abc;     public void doSomething() {         int x = abc.getValue();  //No error checking/lookup etc.         return x + 42;     } ... } AbcLocator locator = ServiceLocator.locator();  IAbc abc = locator.abc();          or AbcFactory.getAbc();

  24. Question? What do we replace these implementations with?

  25. Mock Objects

  26. EasyMock - Overview

  27. EasyMock - Creating Mock Objects Example (Snippet)     ...     IRepository mock = createMock(IRepository.class);

  28. EasyMock - Defining Expectations Example (Snippet)     ...     IRepository mock = createMock(IRepository.class);  expect(mock.getValue()).andReturn(42);

  29. EasyMock - Change Mode Example (Snippet)     ...     IRepository mock = createMock(IRepository.class);      expect(mock.getValue()).andReturn(42); replay(mock);

  30. EasyMock - Manually Inject Mock Example (Snippet)     ...     IRepository mock = createMock(IRepository.class);      expect(mock.getValue()).andReturn(42);     replay(mock); service.setRepository(mock);

  31. EasyMock - Invoke Real Method Example (Snippet)     ...     IRepository mock = createMock(IRepository.class);      expect(mock.getValue()).andReturn(42);     replay(mock);     service.setRepository(mock); x = service.doSomething();

  32. EasyMock - Assert (verify)  Example (Snippet)     ...     IRepository mock = createMock(IRepository.class);      expect(mock.getValue()).andReturn(42);     replay(mock);     service.setRepository(mock);     x = service.doSomething(); veryify(mock);

  33. Question?  How do we know if we have tested everything?

  34. Emma - Code Coverage Overview 

  35. Emma - Instrumenting Classloader 

  36. Emma - Code Coverage Overview 

  37. Break

  38. Example

  39. Example - Terminology CustomerDTO - Data Transfer Object CustomerService - WebService CustomerRepository (Repository Pattern) Customer - Entity (JPA) 

  40. Demo

  41. Pitfalls? • Each client must provide implementation of Abc to X? • Doesn't this break the concept of encapsulation?

  42. Spring IoC Container  a formalized means of composing disparate components into a fully working application ready for use

  43. Questions?

  44. Thanks Please feel free to contact me at: greg.hutchinson@fcc-fac.ca

  45. Two Types of Mock ObjectsImplement your own public class XTest extends TestCase {     public void testDoSomething() {        IAbc mock = new MyMock();        X x = new X();        x.setAbc(mock);        x.doSomething();        assertTrue(x.hasFinished());     } }

  46. Two Types of Mock ObjectsImplement your own public class XTest extends TestCase {     public void testDoSomething() {        IAbc mock = new MyMock();        X x = new X();        x.setAbc(mock);        x.doSomething();        assertTrue(x.hasFinished());     } } public class MyMock implements IAbc {     public int getValue() {        return 42; //Answer to life, the universe ...     }     public boolean isValidObject() {         return false;     } }

More Related