SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
Dennis Byrne
Introduction to Unit Test g
Part 1 of 2
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions
Favorable Unit Test Properties: Encapsulation
Favorable Unit Test Properties: Encapsulation
● A unit test is just another class
● Do test external behavior
○ Return values
○ Interactions with collaborators
● Do not test internal state
○ Private methods
○ Private variables
○ Reflection
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions
Favorable Unit Test Properties: Deterministic
● Sources of Non-determinism
○ Math.random
○ java.util.Random
○ System clock methods
○ Multi-threaded code
○ Networks
Favorable Unit Test Properties: Deterministic
@Test
public void shouldGenerateColorUntestable(){
ColorGenerator bad = new UntestableRandomColorGenerator();
Color nonDeterministic = bad.generate();
// ...
}
Favorable Unit Test Properties: Deterministic
class UntestableRandomColorGenerator implements ColorGenerator {
private final Random _random;
UntestableRandomColorGenerator() {
_random = new Random();
}
public Color generate(){
return Color.color(_random.nextDouble(),
_random.nextDouble(),
_random.nextDouble());
}
}
Favorable Unit Test Properties: Deterministic
@Test
public void shouldGenerateColorTestable(){
// Dependency Injection
Random random = new Random(42);
ColorGenerator good = new TestableRandomColorGenerator(random);
Color deterministic = good.generate();
// ...
}
Favorable Unit Test Properties: Deterministic
class TestableRandomColorGenerator implements ColorGenerator {
private final Random _random;
TestableRandomColorGenerator() { // prod ctor
this(new Random());
}
TestableRandomColorGenerator(Random random) { // test ctor
_random = random;
}
public Color generate(){
return Color.color(_random.nextDouble(), _random.nextDouble(),
_random.nextDouble());
}
}
Favorable Unit Test Properties: Deterministic
class UntestableCalandar implements Calendar {
// ...
public Set<Event> getFutureEvents() {
long nowInMillis = System.currentTimeMillis();
Set<Event> events = new HashSet<Event>();
for (Event event : events){
if(event.getCreationTime() < nowInMillis){
events.add(event);
}
}
return events;
}
}
Favorable Unit Test Properties: Deterministic
@Test public void testFutureEvents(){
// Dependency Injection
Time time = new Time() {
public long currentTimeMillis() {
return 42; // between past & present
}
};
TestableCalendar calendar = new TestableCalendar(time);
// ...
}
Favorable Unit Test Properties: Deterministic
class TestableCalendar implements Calendar {
private final Time _time;
TestableCalendar(Time time) {
_time = time;
}
public Set<Event> getFutureEvents() {
long nowInMillis = _time.currentTimeMillis();
Set<Event> events = new HashSet<Event>();
// ...
return events;
}
}
Favorable Unit Test Properties: Deterministic
interface Time {
long currentTimeMillis();
}
class SystemTime implements Time {
public long currentTimeMillis() {
return System.currentTimeMillis();
}
}
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions
Favorable Unit Test Properties: Side Effects
MutableShared
Favorable Unit Test Properties: Side Effects
● Each unit test is an isolated experiment
● Unit test order should be arbitrary
● Static singletons
● TestNG vs most XUnit test class lifecycle
● Unit Testing and Concurrency agree
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions
Favorable Unit Test Properties: Simplicity
● KISS … Keep It Simple Stupid
● Test code should not reimplement
production
● A unit test should only test one class
● Test method structure
1 - Build inputs
2 - Create the “system under test”
3 - Perform assertions
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions
Favorable Unit Test Properties: Fast
● Avoid the Network
○ Retrieving data from a another online system
○ The LixClient interface enabled unit testing
● Go easy on the file system
○ Loading large amounts of test data from disk
● Avoid large computations
○ Generating combinatorial inputs …
○ Time complexity
○ Memory usage
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions
Unit Test Properties: Assertions
@Test
public void ok(){
Calculator calculator = new Calculator();
Number number = calculator.add(new Integer(3), new Integer(7));
Assert.assertEquals(new Integer(10), number); // three + seven = ten
}
@Test
public void better(){
Calculator calculator = new Calculator();
Number number = calculator.add(new Integer(3), new Integer(7));
Assert.assertEquals(number, new Integer(10), "three + seven = ten");
}
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions
Favorable Unit Test Properties: Name Conventions
Agenda: Favorable Unit Test Properties
● Encapsulation
● Deterministic
● Free of side effects
● Simple
● Fast
● Assertions
● Naming Conventions

Contenu connexe

Tendances (18)

Rx java2 - Should I use it?
Rx java2 - Should I use it?Rx java2 - Should I use it?
Rx java2 - Should I use it?
 
DOCX_FileSavedAsOnBoxNet_28
DOCX_FileSavedAsOnBoxNet_28DOCX_FileSavedAsOnBoxNet_28
DOCX_FileSavedAsOnBoxNet_28
 
TestNG vs. JUnit4
TestNG vs. JUnit4TestNG vs. JUnit4
TestNG vs. JUnit4
 
TestNG Framework
TestNG Framework TestNG Framework
TestNG Framework
 
Testing Spring Applications
Testing Spring ApplicationsTesting Spring Applications
Testing Spring Applications
 
Smidige databaser
Smidige databaserSmidige databaser
Smidige databaser
 
JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8JavaScript Editions ES7, ES8 and ES9 vs V8
JavaScript Editions ES7, ES8 and ES9 vs V8
 
Generics
GenericsGenerics
Generics
 
Lesson3
Lesson3Lesson3
Lesson3
 
Presentation cs313 (1)
Presentation cs313 (1)Presentation cs313 (1)
Presentation cs313 (1)
 
Junit4&testng presentation
Junit4&testng presentationJunit4&testng presentation
Junit4&testng presentation
 
A Chemistry-Inspired Workflow Management System for Scientific Applications o...
A Chemistry-Inspired Workflow Management System for Scientific Applications o...A Chemistry-Inspired Workflow Management System for Scientific Applications o...
A Chemistry-Inspired Workflow Management System for Scientific Applications o...
 
ikp321-04
ikp321-04ikp321-04
ikp321-04
 
NYAN Conference: Debugging asynchronous scenarios in .net
NYAN Conference: Debugging asynchronous scenarios in .netNYAN Conference: Debugging asynchronous scenarios in .net
NYAN Conference: Debugging asynchronous scenarios in .net
 
Hibernate concurrency
Hibernate concurrencyHibernate concurrency
Hibernate concurrency
 
Reactive&amp;reactor
Reactive&amp;reactorReactive&amp;reactor
Reactive&amp;reactor
 
บทที่3
บทที่3บทที่3
บทที่3
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 

En vedette

Agility - Part 1 of 2
Agility - Part 1 of 2Agility - Part 1 of 2
Agility - Part 1 of 2Dennis Byrne
 
JavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and PitfallsJavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and PitfallsDennis Byrne
 
Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)Dennis Byrne
 
The Erlang Programming Language
The Erlang Programming LanguageThe Erlang Programming Language
The Erlang Programming LanguageDennis Byrne
 

En vedette (6)

Agility - Part 1 of 2
Agility - Part 1 of 2Agility - Part 1 of 2
Agility - Part 1 of 2
 
JavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and PitfallsJavaServer Faces Anti-Patterns and Pitfalls
JavaServer Faces Anti-Patterns and Pitfalls
 
git internals
git internalsgit internals
git internals
 
Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)
 
The Erlang Programming Language
The Erlang Programming LanguageThe Erlang Programming Language
The Erlang Programming Language
 
Memory Barriers
Memory BarriersMemory Barriers
Memory Barriers
 

Similaire à Introduction to Unit Testing (Part 1 of 2)

Advance unittest
Advance unittestAdvance unittest
Advance unittestReza Arbabi
 
xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database TestingChris Oldwood
 
Unit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Howsatesgoral
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven developmentStephen Fuqua
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014FalafelSoftware
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...Sergio Arroyo
 
Code Kata: String Calculator in Flex
Code Kata: String Calculator in FlexCode Kata: String Calculator in Flex
Code Kata: String Calculator in FlexChris Farrell
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testabilitydrewz lin
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingSteven Smith
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
Unit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftallanh0526
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsCarol McDonald
 

Similaire à Introduction to Unit Testing (Part 1 of 2) (20)

Advance unittest
Advance unittestAdvance unittest
Advance unittest
 
xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database Testing
 
Unit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Hows
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...Some testing - Everything you should know about testing to go with @pedro_g_s...
Some testing - Everything you should know about testing to go with @pedro_g_s...
 
Unit testing basics
Unit testing basicsUnit testing basics
Unit testing basics
 
Unit testing
Unit testingUnit testing
Unit testing
 
Code Kata: String Calculator in Flex
Code Kata: String Calculator in FlexCode Kata: String Calculator in Flex
Code Kata: String Calculator in Flex
 
谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability谷歌 Scott-lessons learned in testability
谷歌 Scott-lessons learned in testability
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Unit testing in xcode 8 with swift
Unit testing in xcode 8 with swiftUnit testing in xcode 8 with swift
Unit testing in xcode 8 with swift
 
Java Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and TrendsJava Concurrency, Memory Model, and Trends
Java Concurrency, Memory Model, and Trends
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 

Dernier

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 

Dernier (20)

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 

Introduction to Unit Testing (Part 1 of 2)

  • 1. Dennis Byrne Introduction to Unit Test g Part 1 of 2
  • 2. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions
  • 3. Favorable Unit Test Properties: Encapsulation
  • 4. Favorable Unit Test Properties: Encapsulation ● A unit test is just another class ● Do test external behavior ○ Return values ○ Interactions with collaborators ● Do not test internal state ○ Private methods ○ Private variables ○ Reflection
  • 5. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions
  • 6. Favorable Unit Test Properties: Deterministic ● Sources of Non-determinism ○ Math.random ○ java.util.Random ○ System clock methods ○ Multi-threaded code ○ Networks
  • 7. Favorable Unit Test Properties: Deterministic @Test public void shouldGenerateColorUntestable(){ ColorGenerator bad = new UntestableRandomColorGenerator(); Color nonDeterministic = bad.generate(); // ... }
  • 8. Favorable Unit Test Properties: Deterministic class UntestableRandomColorGenerator implements ColorGenerator { private final Random _random; UntestableRandomColorGenerator() { _random = new Random(); } public Color generate(){ return Color.color(_random.nextDouble(), _random.nextDouble(), _random.nextDouble()); } }
  • 9. Favorable Unit Test Properties: Deterministic @Test public void shouldGenerateColorTestable(){ // Dependency Injection Random random = new Random(42); ColorGenerator good = new TestableRandomColorGenerator(random); Color deterministic = good.generate(); // ... }
  • 10. Favorable Unit Test Properties: Deterministic class TestableRandomColorGenerator implements ColorGenerator { private final Random _random; TestableRandomColorGenerator() { // prod ctor this(new Random()); } TestableRandomColorGenerator(Random random) { // test ctor _random = random; } public Color generate(){ return Color.color(_random.nextDouble(), _random.nextDouble(), _random.nextDouble()); } }
  • 11. Favorable Unit Test Properties: Deterministic class UntestableCalandar implements Calendar { // ... public Set<Event> getFutureEvents() { long nowInMillis = System.currentTimeMillis(); Set<Event> events = new HashSet<Event>(); for (Event event : events){ if(event.getCreationTime() < nowInMillis){ events.add(event); } } return events; } }
  • 12. Favorable Unit Test Properties: Deterministic @Test public void testFutureEvents(){ // Dependency Injection Time time = new Time() { public long currentTimeMillis() { return 42; // between past & present } }; TestableCalendar calendar = new TestableCalendar(time); // ... }
  • 13. Favorable Unit Test Properties: Deterministic class TestableCalendar implements Calendar { private final Time _time; TestableCalendar(Time time) { _time = time; } public Set<Event> getFutureEvents() { long nowInMillis = _time.currentTimeMillis(); Set<Event> events = new HashSet<Event>(); // ... return events; } }
  • 14. Favorable Unit Test Properties: Deterministic interface Time { long currentTimeMillis(); } class SystemTime implements Time { public long currentTimeMillis() { return System.currentTimeMillis(); } }
  • 15. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions
  • 16. Favorable Unit Test Properties: Side Effects MutableShared
  • 17. Favorable Unit Test Properties: Side Effects ● Each unit test is an isolated experiment ● Unit test order should be arbitrary ● Static singletons ● TestNG vs most XUnit test class lifecycle ● Unit Testing and Concurrency agree
  • 18. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions
  • 19. Favorable Unit Test Properties: Simplicity ● KISS … Keep It Simple Stupid ● Test code should not reimplement production ● A unit test should only test one class ● Test method structure 1 - Build inputs 2 - Create the “system under test” 3 - Perform assertions
  • 20. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions
  • 21. Favorable Unit Test Properties: Fast ● Avoid the Network ○ Retrieving data from a another online system ○ The LixClient interface enabled unit testing ● Go easy on the file system ○ Loading large amounts of test data from disk ● Avoid large computations ○ Generating combinatorial inputs … ○ Time complexity ○ Memory usage
  • 22. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions
  • 23. Unit Test Properties: Assertions @Test public void ok(){ Calculator calculator = new Calculator(); Number number = calculator.add(new Integer(3), new Integer(7)); Assert.assertEquals(new Integer(10), number); // three + seven = ten } @Test public void better(){ Calculator calculator = new Calculator(); Number number = calculator.add(new Integer(3), new Integer(7)); Assert.assertEquals(number, new Integer(10), "three + seven = ten"); }
  • 24. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions
  • 25. Favorable Unit Test Properties: Name Conventions
  • 26. Agenda: Favorable Unit Test Properties ● Encapsulation ● Deterministic ● Free of side effects ● Simple ● Fast ● Assertions ● Naming Conventions