Professional Documents
Culture Documents
Unit Test
Unit Test
● Unit Testing
● Dependency Injection
● Mock Object
Type of tests
● End to end, black box testing – tests are hardest to
write and run, tests find bugs that are easiest to fix
(bad markup, wrong css rule)
● Functional, wiring test – easier to write tests, find
easy to medium bugs (cannot connect to db,
because server is misconfigured)
● Unit tests – easiest to write, fix logic bugs and
hard-to-spot design problems (violation of single
responsibility principle, hard-coded function calls)
Unit Testing
– wikipeadia.org
Why and Why not Unit Test
Property 1: Isolation
Dependency Injection
– wikipeadia.org
DI: Hidden Dependency
DI: Single Responsibility Principle
DI: Why
● Testable code
● No hidden dependencies = Better API = Maintainability
● All common initialization logic an be extracted
What to avoid:
● Long method call chains
Law Of Demeter
– wikipeadia.org
Law of Demeter
Law of Demeter
Law of Demeter
Control the interaction
Property 2: Speed
Property 3: Repeatability
Test Doubles
– wikipeadia.org
Types of Test Doubles
● Dummy objects – are passed around but never
actually used.
● Fake objects – usually have working
implementations, but usually take some shortcut.
● Stub objects – provide canned answers to calls
made during the test, usually not responding at
all to anything outside what’s programmed in for
the testing.
● Mock objects – are pre-programmed with
expectations which form a specification of the
calls they are expected to receive.
Mock Objects
Mock Objects
Mock Objects
Mock Objects
Mock Objects