Designing Better Software
This document explores the concepts of Jenga Driven Development (JDD) and how to design better software through effective use of mock objects and dependency injection. It addresses the challenges of achieving 100% code coverage and looser coupling in software components. The text includes practical examples of creating and manipulating mock objects with EasyMock and discusses techniques for improving unit testing. Additionally, it emphasizes the importance of clear interface definitions and collaboration between components to enhance software maintainability and testability.
Designing Better Software
E N D
Presentation Transcript
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; } }