complementing unit test with dependency injection and mock ... · complementing unit test with...

81
Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Upload: others

Post on 12-Jun-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Complementing Unit Test with Dependency Injection and Mock Objects

Paulo Caroli & Siddharth Dawara

ThoughtWorks

Page 2: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

About Paulo Caroli

• A technologist at ThoughtWorks US

• Master's Degree in Software Engineering

• Sun Certified J2EE Architect

• More than 13 years in SW Development.

• Agile Techniques, such as XP, TDD, Continuous Integration

• Object Oriented development practices

• More at www.caroli.org

Page 3: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

About Siddharth Dawara

• A geek at ThoughtWorks India

• Web technologies fan

• Agile Techniques, such as XP, TDD, Continuous Integration

• Object Oriented development practices

• More at [email protected]

Page 4: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Agenda

• Unit Test

• Unit Test and TDD

• Testing Dependent Components

• Dependency Injection

• Mock Objects

• Case Study

• Conclusion

• Q & A

Page 5: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Unit Test

Page 6: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Unit Test

“Unit Test is a procedure used to validate that individual units of functional code are working properly. “

Page 7: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Unit Test

• Is usually done by developers

• Improves quality

• Facilitates changes (refactoring)

• Simplifies integration

• Enables automation

• Provides effective system documentation.

• Makes your life simpler

Page 8: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

JUnit

• A open source regression testing framework

• Written by Erich Gamma and Kent Beck

• Used by developers to implement unit tests in Java

• Part of XUnit family

Page 9: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

JUnit

• Provides tools for:

– assertions

– running tests

– aggregating tests (suites)

– reporting results

Page 10: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Sample Functional Code

public class Foo {public Foo() {}

public String helloWorld() {return "Hello World";

}

public boolean truth() {return true;

}

}

Page 11: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

How to Test it?

• Debugger

– Needs human intervention, slow, boring

• Print statements

– Needs human intervention, slow, polluted

code, "Scroll Blindness"

• Use JUnit

– Automatic, quick, no code pollution, fun

Page 12: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Sample JUnit Code

public class FooTest extends TestCase {

private Foo foo = new Foo();;

public void testHelloWorld() {

assertEquals("Hello World",foo.helloWorld());

}

public void testTruth() {

assertTrue(foo.truth());

}

}

Page 13: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

When to Unit Test?

• No Unit Test functional code

(time)

functional code functional code

(time)

functional code

unit test

functional code

unit test

(time)

• Test Last development

(TLD)

– You’ll spend less time

overall

• Test First Development

(TFD)

– Time savings on

debugging

Page 14: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD

Page 15: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Test Driven Development

“Test-Driven Development (TDD) is an evolutionary approach to development which instructs you to have test-first development intent. Basically, you start by writing a test and then you code to elegantly fulfill the test requirements.”

Page 16: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Test Driven Development

• Small successful, tested steps.

• Do the simplest thing that could possibly work.

• The goal is to produce working clean code that fulfills requirements

Page 17: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle

Test Fail Test Pass

2. write

code

1. write test

3. refactor

Page 18: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Step 1 – Write Test Code

• Guarantees that every functional code is testable

• Provides a specification for the functional code

• Helps to think about design

• Ensure the functional code is tangible

Page 19: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Step 2 – Write Functional Code

• Fulfill the requirement (test code)

• Write the simplest solution that works

• Leave Improvements for a later step

• The code written is only designed to pass the test

– no further (and therefore untested code is not

created)

Page 20: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Step 3 – Refactor

• Clean-up the code (test and functional)

• Make sure the code expresses intent

• Remove code smells

• Re-think the design

• Delete unnecessary code

Page 21: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD helps you produce

clean working code that fulfills requirements

Page 22: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle – Step 1

Test Pass

write code

1. write test

Test Fail

3. refactor

Page 23: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

code that fulfills requirements

Page 24: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle – Step 2

Test Fail

2. write code

write test

Test Pass

3. refactor

Page 25: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

working code that fulfills requirements

Page 26: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle – Step 3

Test Fail Test Pass

2. write

code

1. write test

3. refactor

Page 27: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

clean working code that fulfills requirements

Page 28: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Why TDD

• Non TDD way:

lots of code

• TDD way:

clean working code that fulfills requirements

Page 29: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Demo

Page 30: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Task: build a factorial calculator

• Factorial• 0! = 1

• 1! = 1

• 2! = 2 = 2*1

• 3! = 6 = 3*2*1

• 4! = 24 = 4*3*2*1

• 5! = 120 = 5*4*3*2*1

• ...

Page 31: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle – Step 1

Test Pass

write code

1. write test

Test Fail

3. refactor

Page 32: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle – Step 2

Test Fail

2. write code

write test

Test Pass

3. refactor

Page 33: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle – Step 3

Test Fail Test Pass

2. write

code

1. write test

3. refactor

Page 34: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD Cycle Repeated

Test Fail Test Pass

2. write

code

1. write test

3. refactor

Page 35: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

TDD - Bug Fixing

• Create new test to show bug

• Now, fix the bug and watch the bar go green

• Your tests assure the bug won’t reappear

Test Fail Test Pass

2. write code

1. write test

Page 36: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Testing Dependent Components

Page 37: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

But how do I Unit

Test Object

Graphs, Circular

Dependency…

Page 38: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Object Graph

• How to test component A?

• TestA Unit Test, but what about B, C, D…

Page 39: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

My Coworker Component

• Does MyComponentTest have to wait on MyCoworkerComponent to be tested?

MyComponentMyCoworker

Component

Page 40: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Third Party Component

• Does MyComponentTest have to wait on ThirdPartyComponent to be tested?

MyComponentThirdParty

Component

Page 41: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Very Slow Component

• Does MyComponentTest have to wait on VerySlowComponent to be tested?

MyComponentVerySlow

Component

Page 42: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Component with Complex Set up

• Does MyComponentTest really have to setup with ComplexSetUpComponent?

MyComponentComplexSetUp

Component

Page 43: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Component with Exceptional Behavior

• How to test MyComponent for other component exceptional behavior?

MyComponentExceptionalBehavior

Component

Page 44: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Remote Component

• Does MyComponentTest have to wait on RemoteComponent to be tested?

MyComponentRemote

Component

Page 45: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

DB Component

• Does MyComponentTest have to wait on DBComponent to be tested?

MyComponentDB

Component

Page 46: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Circular Dependency

• How to test circular dependency?

Egg Chicken

Page 47: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Dependency

Injection and

Mock Objects

Page 48: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Dependency Injection

Page 49: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Dependency Injection

“Dependency injection avoids dependencies on the implementations of collaborating classes by depending only on interfaces that those classes adhere to.”

Page 50: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Dependency Injection

• Helps design loosely coupled components

• Improves testability

• Simplifies unit testing

• Increases flexibility and maintainability

• Minimizes repetition

• Supplies a plug architecture

• Relies on interfaces

Page 51: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Without Dependency Injection

• MovieLister creates MovieFinderImpl

• MovieLister knows where to find MovieFinderImpl

• MovieLister chooses

MovieFinderImpl

• Hard to test MovieLister

Page 52: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

With Dependency Injection

• Assembler creates MovieFinderImpl

• Assembler injects MovieFinderImpl into

MovieLister

• MovieLister does not choose the concrete

implementation of MovieFinder

• Easier to test MovieLister

Page 53: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Constructor Dependency Injection

public interface Service {

public void doSomething();

}

public class Client {private Service service;

public Client(Service service) {this.service = service;

}

public void doIt() {service.doSomething();

}}

Page 54: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Setter Dependency Injection

public interface Service {

public void doSomething();

}

public class Client {private Service service;

public Client() { }

public setService (Service service) {

this.service = service;

}

public void doIt() {service.doSomething();

}

}

Page 55: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Dependency Injection Containers

• Pico Container

– http://www.picocontainer.org/

• Spring Frameworks

– http://www.springframework.org/

• Guice

– http://code.google.com/p/google-guice/

Page 56: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Mock Objects

Page 57: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Mock Objects

• How to test component A?

• TestA Unit Test, but what about B, C, D…

Page 58: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Mock Objects

unit test

• Use a Unit Test Framework – JUnit - for A unit test

• Use a Mock Object Framework for mocking B and C.

Page 59: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Mock Objects

“Mock Objects are stubs that mimic the behavior of real objects in controlled ways. “

Page 60: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Mock Objects

Mock Objects will enable your unit test to mimic behavior and verify expectations on dependent components.

Page 61: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Mock Objects

Use Mock Objects when testing dependent components, for example:

Object Graph, Circular Dependency, your

Coworker Component, a Third Party

Component, a Very Slow Component, a

Component with Complex Set up, a

Component with Exceptional Behavior, a

Remote Component, a DB Component

Page 62: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Greetings Sample

Page 63: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Multi-language Greeting System

• greetings.sayHello (“English”, “John”);– “Hello John”

• greetings.sayHello (“Italian”, “John”);– “Ciao John”

• greetings.sayHello (“Hindi”, “John”);– “Namaste John”

Page 64: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Greeting Class

public class Greeting {

private ITranslator translator;

public Greeting(ITranslator translator) {

this.translator = translator;

}

public String sayHello(String language, String name) {

return translator.translate(

"English", language, "Hello") + " " + name;

}

}

Page 65: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

ITranslator Interface

public interface ITranslator {

String translate(

String fromLanguage ,

String toLanguage,

String word);

}

Page 66: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

How to test Greeting?

Page 67: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Complement your Unit Testwith Dependency Injection

and Mock Objects

Page 68: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Common Mock Objects Frameworks Functionality

1. Set-up

2. Instantiate the mock object

3. Inject the mock object

4. Set expectations and behavior for the mock object

5. Verify expectations for the mock object

Page 69: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

JMock 2.2.0

Page 70: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

1. Set-up

import junit.framework.TestCase;import org.jmock.Mockery;import org.jmock.Expectations;

public class GreetingTest extends TestCase {public void testGreetingInAnyLanguage() throws Exception {

// set upMockery context = new Mockery();

Page 71: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

2. Instantiate the mock object

import junit.framework.TestCase;import org.jmock.Mockery;import org.jmock.Expectations;

public class GreetingTest extends TestCase {public void testGreetingInAnyLanguage() throws Exception {

// set upMockery context = new Mockery();final ITranslator mockTranslator =

context.mock(

Page 72: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

3. Inject the mock object

import junit.framework.TestCase;import org.jmock.Mockery;import org.jmock.Expectations;

public class GreetingTest extends TestCase {public void testGreetingInAnyLanguage() throws Exception {

// set upMockery context = new Mockery();final ITranslator mockTranslator =

context.mock(

Page 73: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

4. Set expectations and behavior for the mock object

public class GreetingTest extends TestCase {public void testGreetingInAnyLanguage() throws Exception {

// set upMockery context = new Mockery();final ITranslator mockTranslator =

context.mock(

ITranslator.class);

Page 74: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

5. Verify expectations for the mock object

…Mockery context = new Mockery();final ITranslator mockTranslator =

context.mock(

ITranslator.class);

Greeting greeting = new Greeting(mockTranslator);

Page 75: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Mock Objects Sample at:www.mocksamples.org

Page 76: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Dependency Injection Containers Demo

Page 77: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Dependency Injection Containers Sample at:www.helloopensource.org

Page 78: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Session Learning’s

Page 79: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Session Learning's

• Understand the need for Mock Object frameworks

• Peek into the basics of JMock - a Mock Object Open Source framework

• Understand Dependency Injection and how it improves testability

Page 80: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Session Learning's

• Distinguish Constructor and Setter Dependency Injection

• Understand and experience TDD as applied to more complicated unit tests.

• Glimpse of the leading Open Source Dependency Injection Frameworks Spring, PicoContainer and Guice

Page 81: Complementing Unit Test with Dependency Injection and Mock ... · Complementing Unit Test with Dependency Injection and Mock Objects Paulo Caroli & Siddharth Dawara ThoughtWorks

Questions?

THANK YOU!