complementing unit test with dependency injection and mock ... · complementing unit test with...
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/1.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/2.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/3.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/4.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/5.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/6.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/7.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/8.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/9.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/10.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/11.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/12.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/13.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/14.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/15.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/16.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/17.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/18.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/19.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/20.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/21.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/22.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/23.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/24.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/25.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/26.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/27.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/28.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/29.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/30.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/31.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/32.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/33.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/34.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/35.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/36.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/37.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/38.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/39.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/40.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/41.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/42.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/43.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/44.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/45.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/46.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/47.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/48.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/49.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/50.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/51.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/52.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/53.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/54.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/55.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/56.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/57.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/58.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/59.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/60.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/61.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/62.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/63.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/64.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/65.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/66.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/67.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/68.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/69.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/70.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/71.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/72.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/73.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/74.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/75.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/76.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/77.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/78.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/79.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/80.jpg)
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](https://reader030.vdocument.in/reader030/viewer/2022040613/5f05c4177e708231d414994a/html5/thumbnails/81.jpg)
Questions?
THANK YOU!