Designing Better Software

Post on 02-Feb-2016

41 views 0 download

Tags:

description

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. - PowerPoint PPT Presentation

transcript

Designing Better Software

with Mock Objectsusing 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

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

Looser Coupling

Notations 

Notation  IClassName is Interface

public interface  ICustomerRepository {...}

Notation  ClassName is Implementation

public class CustomerRepository {...}

Notation

Class X (Client)

object.doSomething()

Class Ypublic void

doSomething(){

Client – Refers to a Class that invokes behaviour in another Class

Testing

Tenents of Unit Testing

Tenents of Unit Testing

Tenents of Unit Testing

Dependency Injection

What Problem are we solving?

Remove Dependency from X to Abc, Def 

Dependency Injection

Dependency Injection

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();

Question?

What do we replace these implementations with?

Mock Objects

EasyMock - Overview

EasyMock - Creating Mock Objects

Example (Snippet)    ...    IRepository mock = createMock(IRepository.class);

EasyMock - Defining Expectations

Example (Snippet)    ...    IRepository mock = createMock(IRepository.class);     expect(mock.getValue()).andReturn(42);

EasyMock - Change Mode

Example (Snippet)    ...    IRepository mock = createMock(IRepository.class);     expect(mock.getValue()).andReturn(42);    replay(mock);

EasyMock - Manually Inject Mock

Example (Snippet)    ...    IRepository mock = createMock(IRepository.class);     expect(mock.getValue()).andReturn(42);    replay(mock);    service.setRepository(mock);    

EasyMock - Invoke Real Method

Example (Snippet)    ...    IRepository mock = createMock(IRepository.class);     expect(mock.getValue()).andReturn(42);    replay(mock);    service.setRepository(mock);    x = service.doSomething();    

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);    

Question? 

How do we know if we have tested everything?

Emma - Code Coverage Overview 

Emma - Instrumenting Classloader 

Emma - Code Coverage Overview 

    

Break

Example

Example - Terminology

CustomerDTO - Data Transfer ObjectCustomerService - WebServiceCustomerRepository (Repository Pattern)Customer - Entity (JPA) 

Demo

Pitfalls?

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

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

Questions?

Thanks

Please feel free to contact me at:

greg.hutchinson@fcc-fac.ca

Two Types of Mock ObjectsImplement your ownpublic class XTest extends TestCase {    public void testDoSomething() {       IAbc mock = new MyMock();       X x = new X();       x.setAbc(mock);

       x.doSomething();       assertTrue(x.hasFinished());    }}

Two Types of Mock ObjectsImplement your ownpublic 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;    }}