Unit testing using Mock objects and dependency injection Rajasekhar Reddy, Talentica Software
Agenda <ul><ul><li>Unit testing </li></ul></ul><ul><ul><li>Mock objects </li></ul></ul><ul><ul><li>Dependency injection </...
Unit Testing <ul><ul><li>Unit testing is a method by which individual units of source code are tested to determine if they...
Features of good unit tests <ul><ul><li>They should be fast </li></ul></ul><ul><ul><li>They should never use </li></ul></u...
Benefits of unit testing <ul><ul><li>Facilitates change </li></ul></ul><ul><ul><ul><li>  Unit testing allows the programme...
Mock objects <ul><ul><li>Mock objects are simulated objects that mimic the behavior of real objects </li></ul></ul><ul><ul...
Dependency Injection <ul><ul><li>Dependency injection means giving an object its instance variables.   </li></ul></ul><ul>...
Why developers hate unit tests <ul><ul><li>Have to write more code </li></ul></ul><ul><ul><li>Wants to jump to cool new fe...
Source Code <ul><li>https://github.com/ynrajasekhar/utdemo </li></ul>
Upcoming SlideShare
Loading in …5
×

Unit testing using Mock objects and dependency injection

2,143 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,143
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit testing using Mock objects and dependency injection

  1. 1. Unit testing using Mock objects and dependency injection Rajasekhar Reddy, Talentica Software
  2. 2. Agenda <ul><ul><li>Unit testing </li></ul></ul><ul><ul><li>Mock objects </li></ul></ul><ul><ul><li>Dependency injection </li></ul></ul><ul><ul><li>Automated dependency injection </li></ul></ul>
  3. 3. Unit Testing <ul><ul><li>Unit testing is a method by which individual units of source code are tested to determine if they are fit for use. </li></ul></ul><ul><ul><li>A unit smallest amount of testable code. Often a single method/function </li></ul></ul>
  4. 4. Features of good unit tests <ul><ul><li>They should be fast </li></ul></ul><ul><ul><li>They should never use </li></ul></ul><ul><ul><ul><li>A database </li></ul></ul></ul><ul><ul><ul><li>An app server (or server of any kind) </li></ul></ul></ul><ul><ul><ul><li>File/Network I/O or file system </li></ul></ul></ul>
  5. 5. Benefits of unit testing <ul><ul><li>Facilitates change </li></ul></ul><ul><ul><ul><li>  Unit testing allows the programmer to refactor code at a later date, and make sure the module still works correctly (e.g., in regression testing). Whenever a change causes a fault, it can be quickly identified and fixed. </li></ul></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><ul><ul><li>Unit testing provides a sort of living documentation of the system. Developers looking to learn what functionality is provided by a unit and how to use it can look at the unit tests to gain a basic understanding of the unit's API.  </li></ul></ul></ul><ul><ul><li>Design improvements </li></ul></ul>
  6. 6. Mock objects <ul><ul><li>Mock objects are simulated objects that mimic the behavior of real objects </li></ul></ul><ul><ul><li>  If an object has any of the following characteristics, it may be useful to use a mock object in its place: </li></ul></ul><ul><ul><ul><li>supplies non-deterministic results (e.g. the current time or the current temperature); </li></ul></ul></ul><ul><ul><ul><li>has states that are difficult to create or reproduce (e.g. a network error); </li></ul></ul></ul><ul><ul><ul><li>is slow (e.g. a complete database, which would have to be initialized before the test); </li></ul></ul></ul>
  7. 7. Dependency Injection <ul><ul><li>Dependency injection means giving an object its instance variables.  </li></ul></ul><ul><ul><li>Manual </li></ul></ul><ul><ul><ul><li>Simple: Nothing to learn, no dependencies. </li></ul></ul></ul><ul><ul><ul><li>No reflection magic: In IDE it is easy to find out who calls the constructors. </li></ul></ul></ul><ul><ul><ul><li>Even developers who do not understand DI can follow and contribute to projects. </li></ul></ul></ul><ul><ul><li>Automated </li></ul></ul><ul><ul><ul><li>Using manual caller has to know about all the dependencies, using automated DI it will inject all the dependencies so caller do not have to know about dependencies </li></ul></ul></ul>
  8. 8. Why developers hate unit tests <ul><ul><li>Have to write more code </li></ul></ul><ul><ul><li>Wants to jump to cool new feature quickly </li></ul></ul><ul><ul><li>  requires them to face their own potentially imperfect code </li></ul></ul>
  9. 9. Source Code <ul><li>https://github.com/ynrajasekhar/utdemo </li></ul>

×