SlideShare a Scribd company logo
1 of 39
Download to read offline
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

More Related Content

What's hot

Beginning iOS unit testing
Beginning iOS unit testingBeginning iOS unit testing
Beginning iOS unit testingski081
 
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 2015Buşra Deniz, CSM
 
TestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKETestNG Session presented in Xebia XKE
TestNG Session presented in Xebia XKEAbhishek Yadav
 
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
 
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 frameworkBugRaptors
 
TestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaTestNG Annotations in Selenium | Edureka
TestNG Annotations in Selenium | EdurekaEdureka!
 
TestNG Session presented in PB
TestNG Session presented in PBTestNG Session presented in PB
TestNG Session presented in PBAbhishek Yadav
 

What's hot (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
 

Similar to 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 projectJohannes Hoppe
 
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!Return on Intelligence
 
Loopt unit test experiences
Loopt unit test experiencesLoopt unit test experiences
Loopt unit test experiencesHeine Frifeldt
 
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 2012Hong Le Van
 
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 sdetDevLabs Alliance
 
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 HudsonZbyszek Mockun
 
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...suwalki24.pl
 
Introduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnitIntroduction To UnitTesting & JUnit
Introduction To UnitTesting & JUnitMindfire Solutions
 
Beautfiul world of Flutter Testing
Beautfiul world of Flutter TestingBeautfiul world of Flutter Testing
Beautfiul world of Flutter TestingMax Weber
 
Unit Testing and Why it Matters
Unit Testing and Why it MattersUnit Testing and Why it Matters
Unit Testing and Why it MattersyahyaSadiiq
 
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.Deepak Singhvi
 
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 MuleSoftJitendra Bafna
 
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, Mockingmrjawright
 
Dependency Injection in .NET applications
Dependency Injection in .NET applicationsDependency Injection in .NET applications
Dependency Injection in .NET applicationsBabak Naffas
 
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 FrameworksKunal Ashar
 
Testing your applications with mbunit
Testing your applications with mbunitTesting your applications with mbunit
Testing your applications with mbunitBen Hall
 
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++Hong Le Van
 

Similar to 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
 
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
 
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...
 
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++
 

More from Johannes Hoppe

2017 - NoSQL Vorlesung Mosbach
2017 - NoSQL Vorlesung Mosbach2017 - NoSQL Vorlesung Mosbach
2017 - NoSQL Vorlesung MosbachJohannes Hoppe
 
Einführung in Angular 2
Einführung in Angular 2Einführung in Angular 2
Einführung in Angular 2Johannes Hoppe
 
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 IonicJohannes Hoppe
 
2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung Mosbach2015 02-09 - NoSQL Vorlesung Mosbach
2015 02-09 - NoSQL Vorlesung MosbachJohannes Hoppe
 
2012-06-25 - MapReduce auf Azure
2012-06-25 - MapReduce auf Azure2012-06-25 - MapReduce auf Azure
2012-06-25 - MapReduce auf AzureJohannes Hoppe
 
2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript Security2013-06-25 - HTML5 & JavaScript Security
2013-06-25 - HTML5 & JavaScript SecurityJohannes Hoppe
 
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 JavaScriptJohannes Hoppe
 
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 JavaScriptJohannes Hoppe
 
2013 05-03 - HTML5 & JavaScript Security
2013 05-03 -  HTML5 & JavaScript Security2013 05-03 -  HTML5 & JavaScript Security
2013 05-03 - HTML5 & JavaScript SecurityJohannes Hoppe
 
2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL Spartakiade2013-03-23 - NoSQL Spartakiade
2013-03-23 - NoSQL SpartakiadeJohannes Hoppe
 
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 dbJohannes Hoppe
 
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 PracticesJohannes 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 & WebGLJohannes 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 MongodbJohannes Hoppe
 
2012-09-18 - HTML5 & WebGL
2012-09-18 - HTML5 & WebGL2012-09-18 - HTML5 & WebGL
2012-09-18 - HTML5 & WebGLJohannes Hoppe
 
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 & MongoDBJohannes Hoppe
 
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)Johannes Hoppe
 
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 MongoDBJohannes 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 MongoDBJohannes Hoppe
 

More from 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
 

Recently uploaded

UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxUdaiappa Ramachandran
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDELiveplex
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?IES VE
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Brian Pichman
 

Recently uploaded (20)

UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Building AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptxBuilding AI-Driven Apps Using Semantic Kernel.pptx
Building AI-Driven Apps Using Semantic Kernel.pptx
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
20230104 - machine vision
20230104 - machine vision20230104 - machine vision
20230104 - machine vision
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDEADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
ADOPTING WEB 3 FOR YOUR BUSINESS: A STEP-BY-STEP GUIDE
 
How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?How Accurate are Carbon Emissions Projections?
How Accurate are Carbon Emissions Projections?
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )Building Your Own AI Instance (TBLC AI )
Building Your Own AI Instance (TBLC AI )
 

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