Unit testing using Mock objects and dependency injection
Upcoming SlideShare
Loading in...5
×
 

Unit testing using Mock objects and dependency injection

on

  • 2,109 views

 

Statistics

Views

Total Views
2,109
Views on SlideShare
2,105
Embed Views
4

Actions

Likes
0
Downloads
6
Comments
0

1 Embed 4

http://www.linkedin.com 4

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Unit testing using Mock objects and dependency injection Unit testing using Mock objects and dependency injection Presentation Transcript

  • Unit testing using Mock objects and dependency injection Rajasekhar Reddy, Talentica Software
  • Agenda
      • Unit testing
      • Mock objects
      • Dependency injection
      • Automated dependency injection
  • Unit Testing
      • Unit testing is a method by which individual units of source code are tested to determine if they are fit for use.
      • A unit smallest amount of testable code. Often a single method/function
  • Features of good unit tests
      • They should be fast
      • They should never use
        • A database
        • An app server (or server of any kind)
        • File/Network I/O or file system
  • Benefits of unit testing
      • Facilitates change
        •   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.
      • Documentation
        • 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. 
      • Design improvements
  • Mock objects
      • Mock objects are simulated objects that mimic the behavior of real objects
      •   If an object has any of the following characteristics, it may be useful to use a mock object in its place:
        • supplies non-deterministic results (e.g. the current time or the current temperature);
        • has states that are difficult to create or reproduce (e.g. a network error);
        • is slow (e.g. a complete database, which would have to be initialized before the test);
  • Dependency Injection
      • Dependency injection means giving an object its instance variables. 
      • Manual
        • Simple: Nothing to learn, no dependencies.
        • No reflection magic: In IDE it is easy to find out who calls the constructors.
        • Even developers who do not understand DI can follow and contribute to projects.
      • Automated
        • 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
  • Why developers hate unit tests
      • Have to write more code
      • Wants to jump to cool new feature quickly
      •   requires them to face their own potentially imperfect code
  • Source Code
    • https://github.com/ynrajasekhar/utdemo