SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Mock Objects,
        Design and
Dependency Inverting Principle




                           P. Heinonen 2011
What are mock objects?
• Object oriented crash test dummies
• In a unit test mock objects can simulate the
  behavior of complex, real (non-mock) objects
• technique that allow you to isolate classes from
  their dependencies for testing purposes.
• allows fine-grained testing even before the
  dependent classes are fully implemented
• normal assertion in unit test verifies the state but
  with Mock objects test verifies behavior
• many practices in TDD and BDD
Mocks and Stubs
•   A mock is an object that
     – can have expectations
     – will verify that the expected actions have indeed occurred.
     – Is pre-programmed with expectations which form a specification of the calls they are expected
       to receive
•   A stub is an object that
     – you use in order to pass to the code under test.
     – which you can setup expectations, so it would act in certain ways, but those expectations will
       never be verified. A stub's properties will automatically behave like normal properties, and
       you can't set expectations on them.
     – provide canned answers to calls made during the test, usually not responding at all to anything
       outside what's programmed in for the test.

•   If you want to verify the behavior of the system under test, you will use a mock
    with the appropriate expectation, and verify that. If you want just to pass a value
    that may need to act in a certain way, but isn't the focus of this test, you will use a
    stub.
    IMPORTANT: A stub will never cause a test to fail.
When to mock?
• when creating effective unit tests
• when testing object behaving
• when a real object is impractical or impossible to incorporate into a
  unit test
• For example in cases where real-object:
    – 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)
    – does not yet exist or may change behavior
    – would have to include information and methods exclusively for testing
      purposes (and not for its actual task)
Unit test principles
• Test only one thing per test
• each unit test should validate no more than
  one significant interaction with another
  object. This generally implies that a given test
  should have no more than one mock object,
  but it may have several stubs, as needed
• Unit test is test which is only in memory, is fast
  and repeatable and does not touch in any
  external resources
Mocking drives your design
• Mocks force you to think dependencies that
  your classes and methods have.
• It forces to think about coupling between
  classes and drives the design to better one.
• Loosely coupled design makes testing and
  using Mock objects much easier.
• Thinking design early helps you to easily
  manage code changes over time.
Mocking Frameworks
•   Moq
•   Rhino Mocks
•   EasyMock
•   TypeMock
•   Microsoft Fakes (formerly Moles)
•   NMock
Unit test with Rhino Mock
[Test(Description = @"Registering new call and saves the call to the CallRepository")]
public void RegisterNewWorkshopCall_SaveTheCall_WhenCallIsValid()
{
  // There should be three steps in good unit test: Arrange, Act, Assert. "AAA syntax"
  // First you arrange the state, then you execute the code under test and
  // finally you assert that the expected state change or behavior happened.

    // ARRANGE
    ICallRepository mockCallRepository = MockRepository.GenerateMock<ICallRepository>();
    ICallValidator mockCallValidator = MockRepository.GenerateMock<ICallValidator>();
    // controlling program flow by specifying stub
    mockCallValidator.Stub(x => x.IsValid(Arg<Call>.Is.Anything)).Return(true);

    // create SUT (System Under Test with dependency Injecting two mocks)
    var callRegistrationService = new CallRegistrationService(mockCallRepository, mockCallValidator);
    var call = new Call{Id = 123,Gateway = Gateway.EOBD,Parameter = new Parameter()};

    // ACT
    callRegistrationService.RegisterNewCall(call);

    // ASSERT
    mockCallRepository.AssertWasCalled(x => x.Save(call));
}
For the sake of good design
• Good design is better than bad design
• Loosely coupled objects are usually a better
  design than tightly coupled objects
• Testing improves code quality and developer
  efficiency over time
• Testing is easier with a loosely coupled designs
• Managing changes are easier when you have
  good sets of unit tests
Dependency Inversion Principle
• Inverting the design where lower level modules
  defining interface that higher level modules depend
  on.
• So we get; higher level modules which define
  interfaces that lower level modules implement

        ”High-level modules should not depend on low-
        level modules. Both should depend on
        abstractions. Abstraction should not depend upon
        details. Details should depend upon abstractions.”
        - Founder of SOLID principles, Bob Martin
Problem
High level class have to be changed and updated every time the low-level-class interface
is changed, added or removed. So, High level class is not reusable when low-level-classes
defines interfaces which high level class is depended on.



                                   High Level Class
Higher layer




                         Interface                    Interface


Lower layer
                     Low Level Class             Low Level Class
Solution: Inverting Dependency
  High level class defines interface which low-level-classes implement.
  Basically what High level class tells to low-level-classes is that:
  ”you have to implement this interface if I’m going to use you to do this job”.
  Also, now it is possible to add more low-level implementation without changing
  the High level classHigh level class becomes reusable.



                    High Level Class                Interface
Higher layer




                               Low Level Class
                                Low Level Class
                                  Low Level Class
Lower layer                        Low Level Class
Dependency Inversion principle
              example
 WRONG!

Button       Lamp




                          RIGHT!

                     Button        IButtonClient



                    ButtonImpl        Lamp
Inversion of Control
• ”IoC” is a high level practice and design guideline for removing
  dependencies in your code.
• aka. Hollywood principle: ”Don’t call us, we call you”
• Can be used in different ways
    – Control over interface between two systems or components
    – Control over the flow of an application
    – Control over dependency creation and binding
• Serves the following purposes:
    – There is a decoupling of the execution of a certain task from
      implementation.
    – Every module can focus on what it is designed for.
    – Modules make no assumptions about what other systems do but rely
      on their contracts.
    – Replacing modules has no side effect on other modules.
Example of IoC patterns
•   Dependency Inversion Principle ”DIP”
•   Interface Inversion
•   Flow Inversion (not explained here)
•   Create Inversion
Interface Inversion
This is anti-pattern: Don’t over use Interfaces




     IHuman                GameOfLife
        Human


         IElephant
           Elephant


                           IGiraffe
                              Giraffe
Interface Inversion
 Interfaces should have more than 1 implementation!


                                       GameOfLife

              IMammal




Human         Elephant       Giraffe
Provider model

Provider         Consumer
 Provider
   Provider
     Provider




      IService   Consumer




Provider
 Provider
   Provider
     Provider
Creation Inversion
• Reduce use of switch-statements, eh more than
  that...
• Normally you create object like:
   – MyClass mObject = new MyClass();
• Or with interface:
   – IMyInterface myObject = new MyImplementation();
• With inverting control you will...
   – Create object outside of the class they are being used
   – Create objects outside of objects in some central
     place, like a factory patternavoid copy&paste
     coding and tight dependencies+maintenance horror
Some Creation Inversion types
• Factory Pattern
  – Payment payment = InvoiceFactory.CreatePayment();

• Service Locator
  – Payment payment = ServiceLocator.Create(IPayment);

• Dependency Injection ”DI”
  – Payment payment = GetThePayment();
  – InvoiceController controller = new InvoiceController(payment);
  – Moves the creation and binding of a dependency outside of the class
    that depends on it. Common way to create loose coupling
Use common sense with DI
• Dependency Injection drives to good design
  but it has also cons and caveats when using
  too much:
  – Dependency Injection would expose internal
    implementation of class
  – DI also violates encapsulation
  – Dependencies created before needed
  – Large object graphs
• So, Watch out for too many dependencies!
Summary
• Depency Inversion Principle ”DIP” is a principle to
  guide software design to think dependencies between
  higher and lower level modules.
• IoC is a set of practices and patterns which help us to
  achieve the ”DIP” and see the benefits of changing
  dependencies.
• Think before using IoC; do you have good reason, what
  kind of dependency inversion you need and why?
• Every time using IoC you are inverting the Control in
  your design and that mean that you are taking control
  out of objects and interfaces or some other ways
  changing the control.

Weitere ähnliche Inhalte

Was ist angesagt?

Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySqlDhyey Dattani
 
Advanced java
Advanced java Advanced java
Advanced java NA
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming WebStackAcademy
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
2. 型態、變數與運算子
2. 型態、變數與運算子2. 型態、變數與運算子
2. 型態、變數與運算子Justin Lin
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGPrabu U
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework tola99
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design PatternsAnton Keks
 
Java oop by_salem_adrugi
Java oop by_salem_adrugiJava oop by_salem_adrugi
Java oop by_salem_adrugiSalem Adrugi
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptxSameerAhmed593310
 
What is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaWhat is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaEdureka!
 
Java Web Services [1/5]: Introduction to Web Services
Java Web Services [1/5]: Introduction to Web ServicesJava Web Services [1/5]: Introduction to Web Services
Java Web Services [1/5]: Introduction to Web ServicesIMC Institute
 

Was ist angesagt? (20)

Java database connectivity with MySql
Java database connectivity with MySqlJava database connectivity with MySql
Java database connectivity with MySql
 
Advanced java
Advanced java Advanced java
Advanced java
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
JDBC
JDBCJDBC
JDBC
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Introduction to Java Scripting
Introduction to Java ScriptingIntroduction to Java Scripting
Introduction to Java Scripting
 
Java swing
Java swingJava swing
Java swing
 
React + Redux for Web Developers
React + Redux for Web DevelopersReact + Redux for Web Developers
React + Redux for Web Developers
 
2. 型態、變數與運算子
2. 型態、變數與運算子2. 型態、變數與運算子
2. 型態、變數與運算子
 
SERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMINGSERVER SIDE PROGRAMMING
SERVER SIDE PROGRAMMING
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Servlets
ServletsServlets
Servlets
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
 
Java oop by_salem_adrugi
Java oop by_salem_adrugiJava oop by_salem_adrugi
Java oop by_salem_adrugi
 
Java Lambda Expressions.pptx
Java Lambda Expressions.pptxJava Lambda Expressions.pptx
Java Lambda Expressions.pptx
 
Java seminar
Java seminarJava seminar
Java seminar
 
What is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | EdurekaWhat is Dependency Injection in Spring Boot | Edureka
What is Dependency Injection in Spring Boot | Edureka
 
Java Web Services [1/5]: Introduction to Web Services
Java Web Services [1/5]: Introduction to Web ServicesJava Web Services [1/5]: Introduction to Web Services
Java Web Services [1/5]: Introduction to Web Services
 

Ähnlich wie Mock Objects, Design and Dependency Inversion Principle

Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
MongoDB World 2018: Tutorial - MongoDB Meets Chaos Monkey
MongoDB World 2018: Tutorial - MongoDB Meets Chaos MonkeyMongoDB World 2018: Tutorial - MongoDB Meets Chaos Monkey
MongoDB World 2018: Tutorial - MongoDB Meets Chaos MonkeyMongoDB
 
Writing Testable Code
Writing Testable CodeWriting Testable Code
Writing Testable Codejameshalsall
 
Weekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design PatternWeekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design PatternNguyen Trung Kien
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaErick M'bwana
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversionchhabraravish23
 
DIC To The Limit – deSymfonyDay, Barcelona 2014
DIC To The Limit – deSymfonyDay, Barcelona 2014DIC To The Limit – deSymfonyDay, Barcelona 2014
DIC To The Limit – deSymfonyDay, Barcelona 2014Ronny López
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion PrincipleShahriar Hyder
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4Billie Berzinskas
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesQamar Abbas
 
Test Driven Development - Workshop
Test Driven Development - WorkshopTest Driven Development - Workshop
Test Driven Development - WorkshopAnjana Somathilake
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere Miklos Csere
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolatorMaslowB
 
Resilience and chaos engineering
Resilience and chaos engineeringResilience and chaos engineering
Resilience and chaos engineeringEric Wyles
 
How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?satejsahu
 

Ähnlich wie Mock Objects, Design and Dependency Inversion Principle (20)

Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
MongoDB World 2018: Tutorial - MongoDB Meets Chaos Monkey
MongoDB World 2018: Tutorial - MongoDB Meets Chaos MonkeyMongoDB World 2018: Tutorial - MongoDB Meets Chaos Monkey
MongoDB World 2018: Tutorial - MongoDB Meets Chaos Monkey
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Writing Testable Code
Writing Testable CodeWriting Testable Code
Writing Testable Code
 
Weekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design PatternWeekly Meeting: Basic Design Pattern
Weekly Meeting: Basic Design Pattern
 
Unit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - KenyaUnit testing and mocking in Python - PyCon 2018 - Kenya
Unit testing and mocking in Python - PyCon 2018 - Kenya
 
Dependency injection and inversion
Dependency injection and inversionDependency injection and inversion
Dependency injection and inversion
 
DIC To The Limit – deSymfonyDay, Barcelona 2014
DIC To The Limit – deSymfonyDay, Barcelona 2014DIC To The Limit – deSymfonyDay, Barcelona 2014
DIC To The Limit – deSymfonyDay, Barcelona 2014
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Dependency Inversion Principle
Dependency Inversion PrincipleDependency Inversion Principle
Dependency Inversion Principle
 
Coldbox developer training – session 4
Coldbox developer training – session 4Coldbox developer training – session 4
Coldbox developer training – session 4
 
Mobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelinesMobile App Architectures & Coding guidelines
Mobile App Architectures & Coding guidelines
 
Test Driven Development - Workshop
Test Driven Development - WorkshopTest Driven Development - Workshop
Test Driven Development - Workshop
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Unit testing
Unit testingUnit testing
Unit testing
 
Mvvm basics
Mvvm basicsMvvm basics
Mvvm basics
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Resilience and chaos engineering
Resilience and chaos engineeringResilience and chaos engineering
Resilience and chaos engineering
 
How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?
 

Kürzlich hochgeladen

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

Kürzlich hochgeladen (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 

Mock Objects, Design and Dependency Inversion Principle

  • 1. Mock Objects, Design and Dependency Inverting Principle P. Heinonen 2011
  • 2. What are mock objects? • Object oriented crash test dummies • In a unit test mock objects can simulate the behavior of complex, real (non-mock) objects • technique that allow you to isolate classes from their dependencies for testing purposes. • allows fine-grained testing even before the dependent classes are fully implemented • normal assertion in unit test verifies the state but with Mock objects test verifies behavior • many practices in TDD and BDD
  • 3. Mocks and Stubs • A mock is an object that – can have expectations – will verify that the expected actions have indeed occurred. – Is pre-programmed with expectations which form a specification of the calls they are expected to receive • A stub is an object that – you use in order to pass to the code under test. – which you can setup expectations, so it would act in certain ways, but those expectations will never be verified. A stub's properties will automatically behave like normal properties, and you can't set expectations on them. – provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. • If you want to verify the behavior of the system under test, you will use a mock with the appropriate expectation, and verify that. If you want just to pass a value that may need to act in a certain way, but isn't the focus of this test, you will use a stub. IMPORTANT: A stub will never cause a test to fail.
  • 4. When to mock? • when creating effective unit tests • when testing object behaving • when a real object is impractical or impossible to incorporate into a unit test • For example in cases where real-object: – 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) – does not yet exist or may change behavior – would have to include information and methods exclusively for testing purposes (and not for its actual task)
  • 5. Unit test principles • Test only one thing per test • each unit test should validate no more than one significant interaction with another object. This generally implies that a given test should have no more than one mock object, but it may have several stubs, as needed • Unit test is test which is only in memory, is fast and repeatable and does not touch in any external resources
  • 6. Mocking drives your design • Mocks force you to think dependencies that your classes and methods have. • It forces to think about coupling between classes and drives the design to better one. • Loosely coupled design makes testing and using Mock objects much easier. • Thinking design early helps you to easily manage code changes over time.
  • 7. Mocking Frameworks • Moq • Rhino Mocks • EasyMock • TypeMock • Microsoft Fakes (formerly Moles) • NMock
  • 8. Unit test with Rhino Mock [Test(Description = @"Registering new call and saves the call to the CallRepository")] public void RegisterNewWorkshopCall_SaveTheCall_WhenCallIsValid() { // There should be three steps in good unit test: Arrange, Act, Assert. "AAA syntax" // First you arrange the state, then you execute the code under test and // finally you assert that the expected state change or behavior happened. // ARRANGE ICallRepository mockCallRepository = MockRepository.GenerateMock<ICallRepository>(); ICallValidator mockCallValidator = MockRepository.GenerateMock<ICallValidator>(); // controlling program flow by specifying stub mockCallValidator.Stub(x => x.IsValid(Arg<Call>.Is.Anything)).Return(true); // create SUT (System Under Test with dependency Injecting two mocks) var callRegistrationService = new CallRegistrationService(mockCallRepository, mockCallValidator); var call = new Call{Id = 123,Gateway = Gateway.EOBD,Parameter = new Parameter()}; // ACT callRegistrationService.RegisterNewCall(call); // ASSERT mockCallRepository.AssertWasCalled(x => x.Save(call)); }
  • 9. For the sake of good design • Good design is better than bad design • Loosely coupled objects are usually a better design than tightly coupled objects • Testing improves code quality and developer efficiency over time • Testing is easier with a loosely coupled designs • Managing changes are easier when you have good sets of unit tests
  • 10. Dependency Inversion Principle • Inverting the design where lower level modules defining interface that higher level modules depend on. • So we get; higher level modules which define interfaces that lower level modules implement ”High-level modules should not depend on low- level modules. Both should depend on abstractions. Abstraction should not depend upon details. Details should depend upon abstractions.” - Founder of SOLID principles, Bob Martin
  • 11. Problem High level class have to be changed and updated every time the low-level-class interface is changed, added or removed. So, High level class is not reusable when low-level-classes defines interfaces which high level class is depended on. High Level Class Higher layer Interface Interface Lower layer Low Level Class Low Level Class
  • 12. Solution: Inverting Dependency High level class defines interface which low-level-classes implement. Basically what High level class tells to low-level-classes is that: ”you have to implement this interface if I’m going to use you to do this job”. Also, now it is possible to add more low-level implementation without changing the High level classHigh level class becomes reusable. High Level Class Interface Higher layer Low Level Class Low Level Class Low Level Class Lower layer Low Level Class
  • 13. Dependency Inversion principle example WRONG! Button Lamp RIGHT! Button IButtonClient ButtonImpl Lamp
  • 14. Inversion of Control • ”IoC” is a high level practice and design guideline for removing dependencies in your code. • aka. Hollywood principle: ”Don’t call us, we call you” • Can be used in different ways – Control over interface between two systems or components – Control over the flow of an application – Control over dependency creation and binding • Serves the following purposes: – There is a decoupling of the execution of a certain task from implementation. – Every module can focus on what it is designed for. – Modules make no assumptions about what other systems do but rely on their contracts. – Replacing modules has no side effect on other modules.
  • 15. Example of IoC patterns • Dependency Inversion Principle ”DIP” • Interface Inversion • Flow Inversion (not explained here) • Create Inversion
  • 16. Interface Inversion This is anti-pattern: Don’t over use Interfaces IHuman GameOfLife Human IElephant Elephant IGiraffe Giraffe
  • 17. Interface Inversion Interfaces should have more than 1 implementation! GameOfLife IMammal Human Elephant Giraffe
  • 18. Provider model Provider Consumer Provider Provider Provider IService Consumer Provider Provider Provider Provider
  • 19. Creation Inversion • Reduce use of switch-statements, eh more than that... • Normally you create object like: – MyClass mObject = new MyClass(); • Or with interface: – IMyInterface myObject = new MyImplementation(); • With inverting control you will... – Create object outside of the class they are being used – Create objects outside of objects in some central place, like a factory patternavoid copy&paste coding and tight dependencies+maintenance horror
  • 20. Some Creation Inversion types • Factory Pattern – Payment payment = InvoiceFactory.CreatePayment(); • Service Locator – Payment payment = ServiceLocator.Create(IPayment); • Dependency Injection ”DI” – Payment payment = GetThePayment(); – InvoiceController controller = new InvoiceController(payment); – Moves the creation and binding of a dependency outside of the class that depends on it. Common way to create loose coupling
  • 21. Use common sense with DI • Dependency Injection drives to good design but it has also cons and caveats when using too much: – Dependency Injection would expose internal implementation of class – DI also violates encapsulation – Dependencies created before needed – Large object graphs • So, Watch out for too many dependencies!
  • 22. Summary • Depency Inversion Principle ”DIP” is a principle to guide software design to think dependencies between higher and lower level modules. • IoC is a set of practices and patterns which help us to achieve the ”DIP” and see the benefits of changing dependencies. • Think before using IoC; do you have good reason, what kind of dependency inversion you need and why? • Every time using IoC you are inverting the Control in your design and that mean that you are taking control out of objects and interfaces or some other ways changing the control.