SlideShare ist ein Scribd-Unternehmen logo
1 von 39
STAATLICH
ANERKANNTE
FACHHOCHSCHULE
Author: Dip.-Inf. (FH) Johannes Hoppe
Date: 17.11.2010
01.12.2010
STUDIEREN
UND DURCHSTARTEN.
STAATLICH
ANERKANNTE
FACHHOCHSCHULE
RIA – Rich Internet Applications
Author: Dip.-Inf. (FH) Johannes Hoppe
Date: 17.11.2010
01.12.2010
Unit Tests in General
01
30.01.2015
Folie 3
Unit Tests in General
Dependencies
› Every meaningful piece of software has dependencies
to other pieces of software
30.01.2015
Folie 4
SoftwareA
SoftwareB
SoftwareC
Unit Tests in General
Dependencies
› But usually software is tightly coupled to other software
› Tightly coupled software is untestable!
30.01.2015
Folie 5
SoftwareA
SoftwareB
SoftwareC
Unit Tests in General
Dependencies
› We will concentrate on two types of dependencies:
1. A dependency by parameters:
ClassA { Method(ClassB b) {…}}
30.01.2015
Folie 6
SoftwareA
SoftwareB
SoftwareC
Unit Tests in General
Dependencies
2. A dependency by the internal use of the “new” keyword:
Class A {
Method() {
var c = new ClassC();
}}
30.01.2015
Folie 7
SoftwareA
SoftwareB
SoftwareC
Unit Tests in General
Dependencies – Solution
› Our golden tools:
Interfaces & Dependency Injection
30.01.2015
Folie 8
SoftwareA
ISoftwareB
ISoftwareC
SoftwareB
SoftwareC
Unit Tests in General
Interfaces
› Interfaces are blueprints that describe a real implementation
› Method are going to be independent
from the concrete implementation of the parameter:
ClassA { Method(ISoftwareB b) {…}}
30.01.2015
Folie 9
SoftwareA
ISoftwareB
ISoftwareC
SoftwareB
SoftwareC
Unit Tests in General
Dependency Injection
› We completely avoid the usage of the “new” keyword
› Instead, we are “injecting” the required instance
from outside by using Interfaces
› “Poor Man’s”-DI through constructor (works for UT, but is an anti-pattern!)
› by a framework (here: Unity Application Block):
ClassA {
[Dependency]
public InterfaceC C { private get; set; }
}
30.01.2015
Folie 10
Unit Tests in General
Loosely Coupled
1. If all dependencies are hidden by Interfaces
2. We must not always use real-live objects
3. We could also “cheat” with any object we want
(as long as it is implementing the interface)
 Unit Testing!!
30.01.2015
Folie 11
Unit Tests in General
30.01.2015
Folie 12
Definitions
› SUT == System Under Test
› Needs to be “fooled” so that it things it is talking with real collaborators
› Dummy
› Objects passed around but never actually used
› Used to fill required parameters in Unit Tests
› Fake
› Objects with working implementations, but not suitable for production
(e.g. in memory database with hardcoded data)
› Often used in software prototypes
› Should not be used in unit tests, too
Unit Tests in General
30.01.2015
Folie 13
Definitions
› Stubs
› Simple hand-written test-objects
› Set up the environment for the Unit Test by returning hardcoded values
 can be used for:
state verification
 Determine whether the exercised method worked correctly by
examining the state of the SUT and its collaborators
after the method was exercised.
Unit Tests in General
30.01.2015
Folie 14
Definitions
› Mocks
› More complex test-objects
› pre-programmed with expectations which form a specification of the calls
they are expected to receive*
 can be used for:
behavior verification
 Determine whether the exercised method worked correctly by
checking if the SUT made the
correct calls on the collaborators.
(* according to the definition of Martin Fowler)
Unit Tests in General
Definitions
› Many people (including me) are mixing the terms stub, fake and mock
› Mocking frameworks
› Can create Mocks with a lot of magic inside
› Can be used to build simple stubs, too!
30.01.2015
Folie 15
Unit Tests in General
Glossary
› MsTest – The unit testing framework that comes with Visual Studio
› Nunit – A quite popular unit testing framework which is open source
www.nunit.org
› Moq –A mocking framework that easy to use because of the lambda
expression syntax
www.moq.me
30.01.2015
Folie 16
Unit Tests in General
Questions
?
30.01.2015
Folie 17
Review of our first Unit Test
02
30.01.2015
Folie 18
Unit Tests
30.01.2015
Folie 19
How?
Review of our first Unit Test
Explanation of Code
30.01.2015
Folie 20
Review of our first Unit Test
Dependencies
30.01.2015
Folie 21
WebNoteRepository
IWebNote
IObjectSet<Note>
Review of our first Unit Test
Dependencies
IWebNote – Entity Framework:
IWebNote – by our own Mock:
30.01.2015
Folie 22
WebNoteRepository
IWebNote
Review of our first Unit Test
30.01.2015
Folie 23
WebNoteRepository
IWebNote
IObjectSet<Note>
Dependencies
IObjectSet<Note> – Entity Framework:
IObjectSet<Note> – by our own Mock:
Chaining
› Real live: by poor man’s DI
› Unit Test: automatically by the class WebNoteBaseRepositoryTest
› From which we are inheriting in our UT
Review of our first Unit Test
30.01.2015
Folie 24
Chaining
Unit Tests in General
30.01.2015
Folie 25
Mocked Repository
Base class from which we are inheriting
Required to prepare the mock
Unit Tests
Your tasks from last week:
› Try to write your first set of unit tests
› Work together with you neighbor (2 persons, one team)
› Use the Mock-Framework “Moq” from www.moq.me
› Implement:
 AddNoteTest, EditNoteTest, DeleteNoteTest
 DeleteNoteShouldThrowExceptionTest
 GetNoteShouldNotCallSaveChangesTest
 6 Tests for the Controller
30.01.2015
Folie 26
Unit Tests
03
30.01.2015
Folie 27
Unit Tests
Tests: only with stubs
30.01.2015
Folie 28
Unit Tests
30.01.2015
Folie 29
Unit Tests
30.01.2015
Folie 30
Unit Tests
30.01.2015
Folie 31
Unit Tests
30.01.2015
Folie 32
Unit Tests
30.01.2015
Folie 33
Unit Tests
Tests: with Mocks
30.01.2015
Folie 34
Unit Tests
30.01.2015
Folie 35
Unit Tests
30.01.2015
Folie 36
Unit Tests
30.01.2015
Folie 37
Unit Tests
30.01.2015
Folie 38
behavior verification! 
THANK YOU
FOR YOUR ATTENTION
30.01.2015
Folie 39

Weitere ähnliche Inhalte

Was ist angesagt?

Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
Steven Mak
 

Was ist angesagt? (14)

Beginning iOS unit testing
Beginning iOS unit testingBeginning iOS unit testing
Beginning iOS unit testing
 
Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015Unit Testing on Android - Droidcon Berlin 2015
Unit Testing on Android - Droidcon Berlin 2015
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKE
 
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
 
Unit Testing in Android
Unit Testing in AndroidUnit Testing in Android
Unit Testing in Android
 
Is this how you hate unit testing?
Is this how you hate unit testing?Is this how you hate unit testing?
Is this how you hate unit testing?
 
Introduction of TestNG framework and its benefits over Junit framework
Introduction of TestNG framework and its benefits over Junit frameworkIntroduction of TestNG framework and its benefits over Junit framework
Introduction of TestNG framework and its benefits over Junit framework
 
TestNG
TestNGTestNG
TestNG
 
Test NG Framework Complete Walk Through
Test NG Framework Complete Walk ThroughTest NG Framework Complete Walk Through
Test NG Framework Complete Walk Through
 
TestNg_Overview_Config
TestNg_Overview_ConfigTestNg_Overview_Config
TestNg_Overview_Config
 
TDD - Unit Testing
TDD - Unit TestingTDD - Unit Testing
TDD - Unit Testing
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | Edureka
 
Selenium with java
Selenium with javaSelenium with java
Selenium with java
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PB
 

Ähnlich wie RIA 06 & 07 - Unit Testing in Detail

Ria 04 & 05 - First ASP.NET MVC project
Ria 04 & 05 - First ASP.NET MVC projectRia 04 & 05 - First ASP.NET MVC project
Ria 04 & 05 - First ASP.NET MVC project
Johannes Hoppe
 

Ähnlich wie RIA 06 & 07 - Unit Testing in Detail (20)

Ria 04 & 05 - First ASP.NET MVC project
Ria 04 & 05 - First ASP.NET MVC projectRia 04 & 05 - First ASP.NET MVC project
Ria 04 & 05 - First ASP.NET MVC project
 
Unit Tests? It is Very Simple and Easy!
Unit Tests? It is Very Simple and Easy!Unit Tests? It is Very Simple and Easy!
Unit Tests? It is Very Simple and Easy!
 
Loopt unit test experiences
Loopt unit test experiencesLoopt unit test experiences
Loopt unit test experiences
 
Tutorial test driven development with Visual Studio 2012
Tutorial test driven development with Visual Studio 2012Tutorial test driven development with Visual Studio 2012
Tutorial test driven development with Visual Studio 2012
 
Top 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetTop 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdet
 
Open Source tools in Continuous Integration environment (case study for agil...
Open Source tools in Continuous Integration environment  (case study for agil...Open Source tools in Continuous Integration environment  (case study for agil...
Open Source tools in Continuous Integration environment (case study for agil...
 
Continuous Integration testing based on Selenium and Hudson
Continuous Integration testing based on Selenium and HudsonContinuous Integration testing based on Selenium and Hudson
Continuous Integration testing based on Selenium and Hudson
 
Introduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnitIntroduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnit
 
Beautfiul world of Flutter Testing
Beautfiul world of Flutter TestingBeautfiul world of Flutter Testing
Beautfiul world of Flutter Testing
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Unit Testing and Why it Matters
Unit Testing and Why it MattersUnit Testing and Why it Matters
Unit Testing and Why it Matters
 
Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
 
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoftEngineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
Engineering Student MuleSoft Meetup#4 - API Testing With MuleSoft
 
Test Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, MockingTest Driven Development:Unit Testing, Dependency Injection, Mocking
Test Driven Development:Unit Testing, Dependency Injection, Mocking
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applications
 
The 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web FrameworksThe 2014 Decision Makers Guide to Java Web Frameworks
The 2014 Decision Makers Guide to Java Web Frameworks
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
Munit_in_mule_naveen
Munit_in_mule_naveenMunit_in_mule_naveen
Munit_in_mule_naveen
 
Testing your applications with mbunit
Testing your applications with mbunitTesting your applications with mbunit
Testing your applications with mbunit
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 

Mehr von Johannes Hoppe

2012-10-16 - WebTechCon 2012: HTML5 & WebGL
2012-10-16 - WebTechCon 2012: HTML5 & WebGL2012-10-16 - WebTechCon 2012: HTML5 & WebGL
2012-10-16 - WebTechCon 2012: HTML5 & WebGL
Johannes Hoppe
 
2012-10-12 - NoSQL in .NET - mit Redis und Mongodb
2012-10-12 - NoSQL in .NET - mit Redis und Mongodb2012-10-12 - NoSQL in .NET - mit Redis und Mongodb
2012-10-12 - NoSQL in .NET - mit Redis und Mongodb
Johannes Hoppe
 
2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB
2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB
2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB
Johannes Hoppe
 

Mehr von Johannes Hoppe (20)

2017 - NoSQL Vorlesung Mosbach
2017 - NoSQL Vorlesung Mosbach2017 - NoSQL Vorlesung Mosbach
2017 - NoSQL Vorlesung Mosbach
 
NoSQL - Hands on
NoSQL - Hands onNoSQL - Hands on
NoSQL - Hands on
 
Einführung in Angular 2
Einführung in Angular 2Einführung in Angular 2
Einführung in Angular 2
 
MDC kompakt 2014: Hybride Apps mit Cordova, AngularJS und Ionic
MDC kompakt 2014: Hybride Apps mit Cordova, AngularJS und IonicMDC kompakt 2014: Hybride Apps mit Cordova, AngularJS und Ionic
MDC kompakt 2014: Hybride Apps mit Cordova, AngularJS und Ionic
 
2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach
 
2012-06-25 - MapReduce auf Azure
2012-06-25 - MapReduce auf Azure2012-06-25 - MapReduce auf Azure
2012-06-25 - MapReduce auf Azure
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security
 
2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript2013-06-24 - Software Craftsmanship with JavaScript
2013-06-24 - Software Craftsmanship with JavaScript
 
2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript2013-06-15 - Software Craftsmanship mit JavaScript
2013-06-15 - Software Craftsmanship mit JavaScript
 
2013 05-03 - HTML5 & JavaScript Security
2013 05-03 -  HTML5 & JavaScript Security2013 05-03 -  HTML5 & JavaScript Security
2013 05-03 - HTML5 & JavaScript Security
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade
 
2013 02-26 - Software Tests with Mongo db
2013 02-26 - Software Tests with Mongo db2013 02-26 - Software Tests with Mongo db
2013 02-26 - Software Tests with Mongo db
 
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
2013-02-21 - .NET UG Rhein-Neckar: JavaScript Best Practices
 
2012-10-16 - WebTechCon 2012: HTML5 & WebGL
2012-10-16 - WebTechCon 2012: HTML5 & WebGL2012-10-16 - WebTechCon 2012: HTML5 & WebGL
2012-10-16 - WebTechCon 2012: HTML5 & WebGL
 
2012-10-12 - NoSQL in .NET - mit Redis und Mongodb
2012-10-12 - NoSQL in .NET - mit Redis und Mongodb2012-10-12 - NoSQL in .NET - mit Redis und Mongodb
2012-10-12 - NoSQL in .NET - mit Redis und Mongodb
 
2012-09-18 - HTML5 & WebGL
2012-09-18 - HTML5 & WebGL2012-09-18 - HTML5 & WebGL
2012-09-18 - HTML5 & WebGL
 
2012-09-17 - WDC12: Node.js & MongoDB
2012-09-17 - WDC12: Node.js & MongoDB2012-09-17 - WDC12: Node.js & MongoDB
2012-09-17 - WDC12: Node.js & MongoDB
 
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
2012-08-29 - NoSQL Bootcamp (Redis, RavenDB & MongoDB für .NET Entwickler)
 
2012-05-14 NoSQL in .NET - mit Redis und MongoDB
2012-05-14 NoSQL in .NET - mit Redis und MongoDB2012-05-14 NoSQL in .NET - mit Redis und MongoDB
2012-05-14 NoSQL in .NET - mit Redis und MongoDB
 
2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB
2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB
2012-05-10 - UG Karlsruhe: NoSQL in .NET - mit Redis und MongoDB
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Kürzlich hochgeladen (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 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
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

RIA 06 & 07 - Unit Testing in Detail

  • 1. STAATLICH ANERKANNTE FACHHOCHSCHULE Author: Dip.-Inf. (FH) Johannes Hoppe Date: 17.11.2010 01.12.2010 STUDIEREN UND DURCHSTARTEN.
  • 2. STAATLICH ANERKANNTE FACHHOCHSCHULE RIA – Rich Internet Applications Author: Dip.-Inf. (FH) Johannes Hoppe Date: 17.11.2010 01.12.2010
  • 3. Unit Tests in General 01 30.01.2015 Folie 3
  • 4. Unit Tests in General Dependencies › Every meaningful piece of software has dependencies to other pieces of software 30.01.2015 Folie 4 SoftwareA SoftwareB SoftwareC
  • 5. Unit Tests in General Dependencies › But usually software is tightly coupled to other software › Tightly coupled software is untestable! 30.01.2015 Folie 5 SoftwareA SoftwareB SoftwareC
  • 6. Unit Tests in General Dependencies › We will concentrate on two types of dependencies: 1. A dependency by parameters: ClassA { Method(ClassB b) {…}} 30.01.2015 Folie 6 SoftwareA SoftwareB SoftwareC
  • 7. Unit Tests in General Dependencies 2. A dependency by the internal use of the “new” keyword: Class A { Method() { var c = new ClassC(); }} 30.01.2015 Folie 7 SoftwareA SoftwareB SoftwareC
  • 8. Unit Tests in General Dependencies – Solution › Our golden tools: Interfaces & Dependency Injection 30.01.2015 Folie 8 SoftwareA ISoftwareB ISoftwareC SoftwareB SoftwareC
  • 9. Unit Tests in General Interfaces › Interfaces are blueprints that describe a real implementation › Method are going to be independent from the concrete implementation of the parameter: ClassA { Method(ISoftwareB b) {…}} 30.01.2015 Folie 9 SoftwareA ISoftwareB ISoftwareC SoftwareB SoftwareC
  • 10. Unit Tests in General Dependency Injection › We completely avoid the usage of the “new” keyword › Instead, we are “injecting” the required instance from outside by using Interfaces › “Poor Man’s”-DI through constructor (works for UT, but is an anti-pattern!) › by a framework (here: Unity Application Block): ClassA { [Dependency] public InterfaceC C { private get; set; } } 30.01.2015 Folie 10
  • 11. Unit Tests in General Loosely Coupled 1. If all dependencies are hidden by Interfaces 2. We must not always use real-live objects 3. We could also “cheat” with any object we want (as long as it is implementing the interface)  Unit Testing!! 30.01.2015 Folie 11
  • 12. Unit Tests in General 30.01.2015 Folie 12 Definitions › SUT == System Under Test › Needs to be “fooled” so that it things it is talking with real collaborators › Dummy › Objects passed around but never actually used › Used to fill required parameters in Unit Tests › Fake › Objects with working implementations, but not suitable for production (e.g. in memory database with hardcoded data) › Often used in software prototypes › Should not be used in unit tests, too
  • 13. Unit Tests in General 30.01.2015 Folie 13 Definitions › Stubs › Simple hand-written test-objects › Set up the environment for the Unit Test by returning hardcoded values  can be used for: state verification  Determine whether the exercised method worked correctly by examining the state of the SUT and its collaborators after the method was exercised.
  • 14. Unit Tests in General 30.01.2015 Folie 14 Definitions › Mocks › More complex test-objects › pre-programmed with expectations which form a specification of the calls they are expected to receive*  can be used for: behavior verification  Determine whether the exercised method worked correctly by checking if the SUT made the correct calls on the collaborators. (* according to the definition of Martin Fowler)
  • 15. Unit Tests in General Definitions › Many people (including me) are mixing the terms stub, fake and mock › Mocking frameworks › Can create Mocks with a lot of magic inside › Can be used to build simple stubs, too! 30.01.2015 Folie 15
  • 16. Unit Tests in General Glossary › MsTest – The unit testing framework that comes with Visual Studio › Nunit – A quite popular unit testing framework which is open source www.nunit.org › Moq –A mocking framework that easy to use because of the lambda expression syntax www.moq.me 30.01.2015 Folie 16
  • 17. Unit Tests in General Questions ? 30.01.2015 Folie 17
  • 18. Review of our first Unit Test 02 30.01.2015 Folie 18
  • 20. Review of our first Unit Test Explanation of Code 30.01.2015 Folie 20
  • 21. Review of our first Unit Test Dependencies 30.01.2015 Folie 21 WebNoteRepository IWebNote IObjectSet<Note>
  • 22. Review of our first Unit Test Dependencies IWebNote – Entity Framework: IWebNote – by our own Mock: 30.01.2015 Folie 22 WebNoteRepository IWebNote
  • 23. Review of our first Unit Test 30.01.2015 Folie 23 WebNoteRepository IWebNote IObjectSet<Note> Dependencies IObjectSet<Note> – Entity Framework: IObjectSet<Note> – by our own Mock:
  • 24. Chaining › Real live: by poor man’s DI › Unit Test: automatically by the class WebNoteBaseRepositoryTest › From which we are inheriting in our UT Review of our first Unit Test 30.01.2015 Folie 24
  • 25. Chaining Unit Tests in General 30.01.2015 Folie 25 Mocked Repository Base class from which we are inheriting Required to prepare the mock
  • 26. Unit Tests Your tasks from last week: › Try to write your first set of unit tests › Work together with you neighbor (2 persons, one team) › Use the Mock-Framework “Moq” from www.moq.me › Implement:  AddNoteTest, EditNoteTest, DeleteNoteTest  DeleteNoteShouldThrowExceptionTest  GetNoteShouldNotCallSaveChangesTest  6 Tests for the Controller 30.01.2015 Folie 26
  • 28. Unit Tests Tests: only with stubs 30.01.2015 Folie 28
  • 34. Unit Tests Tests: with Mocks 30.01.2015 Folie 34
  • 39. THANK YOU FOR YOUR ATTENTION 30.01.2015 Folie 39