460 likes | 578 Views
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.
E N D
Designing Better Software with Mock Objects using Dependency Injection
Who is this guy? greg.hutchinson@fcc-fac.ca
NotationIClassName is Interface public interface ICustomerRepository { ... }
Notation ClassName is Implementation public class CustomerRepository { ... }
Client – Refers to a Class that invokes behaviour in another Class Notation Class X (Client) object.doSomething() Class Y public void doSomething() {
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?
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?
Example - Terminology CustomerDTO - Data Transfer Object CustomerService - WebService CustomerRepository (Repository Pattern) Customer - Entity (JPA)
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
Thanks Please feel free to contact me at: greg.hutchinson@fcc-fac.ca
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()); } }
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; } }