SlideShare une entreprise Scribd logo
1  sur  40
Télécharger pour lire hors ligne
05 BARCELONA
David Rodenas, PhD
TDD Course
IMPROVE TESTING
@drpicox
HELLO WORLD
IMPROVE TESTING 2
@drpicox
@drpicox
public class HelloWorld {


public static void main(String[] args) {


System.out.println("Hello World");


}


}


HELLO WORLD
3
IMPROVE TESTING — HELLO WORLD
@drpicox
@drpicox
public class HelloWorld {


public static void main(String[] args) {


System.out.println("Hello World");


}


}


@Test


public void say_hello_world() {


HelloWorld.main(null);


assertThat(???)


}


HELLO WORLD
4
IMPROVE TESTING — HELLO WORLD
@drpicox
@drpicox
public class HelloWorld {


public static void main(String[] args) {


System.out.println("Hello World");


}


}


@Test


public void say_hello_world() {


var buffer = new ByteArrayOutputStream();


var out = new PrintStream(buffer);


System.setOut(out);


HelloWorld.main(null);


assertThat(buffer.toString()).isEqualTo("Hello Worldn");


}
HELLO WORLD
5
IMPROVE TESTING — HELLO WORLD
@drpicox
@drpicox
public class HelloWorld {


public static void main(String[] args) {


System.out.println("Hello World");


}


}


@Test


public void say_hello_world() {


var realOut = System.out;


var buffer = new ByteArrayOutputStream();


var testOut = new PrintStream(buffer);


System.setOut(testOut);


HelloWorld.main(null);


System.setOut(realOut);


assertThat(buffer.toString()).isEqualTo("Hello Worldn");


}
HELLO WORLD
6
IMPROVE TESTING — HELLO WORLD
@drpicox
@drpicox
public class HelloWorld {


private PrintStream out;


public HelloWorld(PrintStream out) {


this.out = out;


}


public void sayHello() {


this.out.println("Hello World");


}


public static void main(String[] args) {


new HelloWorld(System.out).sayHello();


}


}
HELLO WORLD
7
IMPROVE TESTING — HELLO WORLD
@drpicox
@drpicox
@Test


public void say_hello_world() {


var buffer = new ByteArrayOutputStream();


var testOut = new PrintStream(buffer);


new HelloWorld(testOut).sayHello();


assertThat(buffer.toString()).isEqualTo("Hello Worldn");


}
HELLO WORLD
8
IMPROVE TESTING — HELLO WORLD
@drpicox
@drpicox
HELLO WORLD
9
IMPROVE TESTING — HELLO WORLD
public class HelloWorld {


public static void main(String[] args) {


System.out.println("Hello World");


}


}


@Test


public void say_hello_world() {


var buffer = new ByteArrayOutputStream();


var out = new PrintStream(buffer);


System.setOut(out);


HelloWorld.main(null);


assertThat(buffer.toString()).isEqualTo("Hello Worldn");


}
@drpicox
@drpicox
@Test


public void say_hello_world() {


var buffer = new ByteArrayOutputStream();


var testOut = new PrintStream(buffer);


new HelloWorld(testOut).sayHello();


assertThat(buffer.toString()).isEqualTo("Hello Worldn");


}
HELLO WORLD
10
IMPROVE TESTING — HELLO WORLD
@drpicox
PATTERNS
IMPROVE TESTING 11
12
@drpicox
@drpicox
Create one single instance, do not use static
LOWER "S" SINGLETON
13
IMPROVE TESTING — PATTERNS
// wrong


public class House {


private static House instance;


public static House getInstance() {


if (instance == null) instance = new House();


return instance;


}


}


// correct


var house = new House();
@drpicox
@drpicox
Ask for what you operate; do not look for things
LAW OF DEMETER
14
IMPROVE TESTING — PATTERNS
// wrong


house.getDoor().open()


// correct


door.open();
@drpicox
@drpicox
Give me what I need
DEPENDENCY INJECTION
15
IMPROVE TESTING — PATTERNS
// wrong (Java)


public class Clicker {


public void click() {


House.getInstance().getDoor().open();


}


}
// wrong (Javascript)


import house from './house'


export class Clicker {


click() {


house.getDoor().open();


}


}
@drpicox
@drpicox
Give me what I need
DEPENDENCY INJECTION
16
IMPROVE TESTING — PATTERNS
// correct


public class Clicker {


public void click(Door door) {


door.open();


}


}
@drpicox
@drpicox
Give me what I need
DEPENDENCY INJECTION
17
IMPROVE TESTING — PATTERNS
// better (do not breaks method signature)


public class Clicker {


private Door door;


public void Clicker(Door door) {


this.door = door;


}


public void click() {


door.open();


}


}
@drpicox
STATICS ARE LIARS
IMPROVE TESTING 18
@drpicox
@drpicox
Deceptive API
STATICS ARE LIARS
19
IMPROVE TESTING — STATICS ARE LIARS
testCharge() {


var cc = new CreditCard("123...234")


cc.charge(100);


}
Throws an exception... why?
@drpicox
@drpicox
Deceptive API
STATICS ARE LIARS
20
IMPROVE TESTING — STATICS ARE LIARS
testCharge() {


CreditCardProcessor.init(...);


var cc = new CreditCard("123...234")


cc.charge(100);


}
Throws an exception... why?
@drpicox
@drpicox
Deceptive API
STATICS ARE LIARS
21
IMPROVE TESTING — STATICS ARE LIARS
testCharge() {


OfflineQueue.start();


CreditCardProcessor.init(...);


var cc = new CreditCard("123...234")


cc.charge(100);


}
Throws an exception... why?
@drpicox
@drpicox
Deceptive API
STATICS ARE LIARS
22
IMPROVE TESTING — STATICS ARE LIARS
testCharge() {


Database.connect(...);


OfflineQueue.start();


CreditCardProcessor.init(...);


var cc = new CreditCard("123...234")


cc.charge(100);


}
Now works... and makes the charge...
@drpicox
@drpicox
Deceptive API
STATICS ARE LIARS
23
IMPROVE TESTING — STATICS ARE LIARS
testCharge() {


var db = Database();


var q = new OfflineQueue(db);


var ccp = new CreditCardProcessor(q);


var cc = new CreditCard("123...234")


cc.charge(100, ccp);


}
Now works... and makes the charge...
@drpicox
@drpicox
Deceptive API
STATICS ARE LIARS
24
IMPROVE TESTING — STATICS ARE LIARS
testCharge() {


var db = MockDatabase();


var q = new OfflineQueue(db);


var ccp = new CreditCardProcessor(q);


var cc = new CreditCard("123...234")


cc.charge(100, ccp);


assertThat(db).hasCharge(100, cc);


}
@drpicox
AVOID CODE ASSERTIONS
IMPROVE TESTING 25
@drpicox
@drpicox
public class House {


Door door;


Window window;


Roof roof;


Kitchen kitchen;


LivingRoom livingRoom;


BedRoom bedRoom;


House(Door door, Window window, Roof roof,


Kitchen kitchen, LivingRoom livingRoom, BedRoom bedRoom) {


this.door = Assert.notNull(door);


this.window = Assert.notNull(window);


this.roof = Assert.notNull(roof);


this.kitchen = Assert.notNull(kitchen);


this.livingRoom = Assert.notNull(livingRoom);


this.bedRoom = Assert.notNull(bedRoom);


}


void secure() {


door.lock();


window.close();


}


}
26
IMPROVE TESTING — AVOID CODE ASSERTIONS
@drpicox
@drpicox
public class TestHouse {


@Test


testSecureHouse() {


Door door = new Door();


Window window = new Window();


House house = new House(door, window,


new Roof(),


new Kitchen(),


new LivingRoom(),


new BedRoom());


house.secure();


assertTrue(door.isLocked());


assertTrue(window.isClosed());


}


}
27
IMPROVE TESTING — AVOID CODE ASSERTIONS
Irrelevant instances on the test
(what if they had also complex constructor?) 😱
@drpicox
@drpicox
public class TestHouse {


@Test


testSecureHouse() {


Door door = new Door();


Window window = new Window();


House house = new House(door, window, null, null, null, null);


house.secure();


assertTrue(door.isLocked());


assertTrue(window.isClosed());


}


}
28
IMPROVE TESTING — AVOID CODE ASSERTIONS
We do not care, faster and easier to understand
@drpicox
FIXING EXAMPLES
IMPROVE TESTING 29
@drpicox
@drpicox
public class TemperatureLogger implements Logger {


public void log(Notebook notebook) {


var weatherStation = WeatherStation.getInstance();


var thermometer = weatherStation.getThermometer();


var temperature = thermometer.getTemperature();


notebook.annotate(temperature);


}


}


// It is instantiated like this:


var logger = new TemperatureLogger();
TEMPERATURE LOGGER
30
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class TemperatureLogger implements Logger {


private WeatherStation weatherStation;


TemperatureLogger(WeatherStation weatherStation) {


this.weatherStation = weatherStation;


}


public void log(Notebook notebook) {


var thermometer = weatherStation.getThermometer();


var temperature = thermometer.getTemperature();


notebook.annotate(temperature);


}


}


// It is instantiated like this:


var logger = new TemperatureLogger(weatherStation);
TEMPERATURE LOGGER
31
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class TemperatureLogger implements Logger {


private Thermometer thermometer;


TemperatureLogger(Thermometer thermometer) {


this.thermometer = thermometer;


}


public void log(Notebook notebook) {


var temperature = thermometer.getTemperature();


notebook.annotate(temperature);


}


}


// It is instantiated like this:


var logger = new TemperatureLogger(


weatherStation.getThermometer()


);
TEMPERATURE LOGGER
32
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class WheelTester {


private LegalRules legalRules;




public WheelTester(LegalRules legalRules) {


this.legalRules = legalRules;


}




public boolean checkWheels(Car car) {


Thikness thikness = legalRules.getThikness();


List<Wheel> wheels = car.getWheels();


for (Wheel wheel: wheels) {


boolean isOk = wheel.hasThikness(thikness);


if (!isOk) return false;


}


return true;


}


}
WHEEL TESTER
33
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class WheelTester {


private Thikness thikness;




public WheelTester(Thikness thikness) {


this.thikness = thikness;


}




public boolean checkWheels(Car car) {


List<Wheel> wheels = car.getWheels();


for (Wheel wheel: wheels) {


boolean isOk = wheel.hasThikness(thikness);


if (!isOk) return false;


}


return true;


}


}
WHEEL TESTER
34
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class WheelTester {


private Thikness thikness;




public WheelTester(Thikness thikness) {


this.thikness = thikness;


}




public boolean checkWheels(List<Wheel> wheels) {


for (Wheel wheel: wheels) {


boolean isOk = wheel.hasThikness(thikness);


if (!isOk) return false;


}


return true;


}


}
WHEEL TESTER
35
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class HDFormatterA {


private HD hd;




public HDFormatterA() {


hd = new HardDisk();


}


public void doWork() {


hd.open();


hd.format("FAT");


hd.close();


}


}
HD FORMATTER
36
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class HDFormatterB {


private HD hd;


public HDFormatterB() {


hd = HardDisk.getInstance();


}


public void doWork() {


hd.open();


hd.format("FAT");


hd.close();


}


}
HD FORMATTER
37
IMPROVE TESTING — FIXING EXAMPLES
@drpicox
@drpicox
public class HDFormatterC {


private HD hd;


public HDFormatterC() {


hd = ServiceLocator.get(HD.class);


}


public void doWork() {


hd.open();


hd.format("FAT");


hd.close();


}


}
HD FORMATTER
38
IMPROVE TESTING — FIXING EXAMPLES
public void test_hdformatterc() {


var hd = new MockHD();


ServiceLocator.set(HD.class, hd);


new HDFormatterC().doWork();


assertThat(hd.verify()).isTrue();


}
@drpicox
@drpicox
public class HDFormatterD {


private HD hd;


public HDFormatterD(HD hd) {


this.hd = hd;


}


public void doWork() {


hd.open();


hd.format("FAT");


hd.close();


}


}
HD FORMATTER
39
IMPROVE TESTING — FIXING EXAMPLES
public void test_hdformatterd() {


var hd = new MockHD();


new HDFormatterD().doWork(hd);


assertThat(hd.verify()).isTrue();


}
@drpicox
E.O.IMPROVE TESTING

Contenu connexe

Tendances

TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesDavid Rodenas
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
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 patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
 
We Make Debugging Sucks Less
We Make Debugging Sucks LessWe Make Debugging Sucks Less
We Make Debugging Sucks LessAlon Fliess
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydneyjulien.ponge
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctlyDror Helper
 
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"epamspb
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189Mahmoud Samir Fayed
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...julien.ponge
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Knowvilniusjug
 
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
 
#codemotion2016: Everything you should know about testing to go with @pedro_g...
#codemotion2016: Everything you should know about testing to go with @pedro_g...#codemotion2016: Everything you should know about testing to go with @pedro_g...
#codemotion2016: Everything you should know about testing to go with @pedro_g...Sergio Arroyo
 

Tendances (20)

TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD Techniques
 
JS and patterns
JS and patternsJS and patterns
JS and patterns
 
GMock framework
GMock frameworkGMock framework
GMock framework
 
TDD Training
TDD TrainingTDD Training
TDD Training
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
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 patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
We Make Debugging Sucks Less
We Make Debugging Sucks LessWe Make Debugging Sucks Less
We Make Debugging Sucks Less
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydney
 
Spock Framework
Spock FrameworkSpock Framework
Spock Framework
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
#ITsubbotnik Spring 2017: Roman Iovlev "Java edge in test automation"
 
The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189The Ring programming language version 1.6 book - Part 184 of 189
The Ring programming language version 1.6 book - Part 184 of 189
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
Java 7 LavaJUG
Java 7 LavaJUGJava 7 LavaJUG
Java 7 LavaJUG
 
Developer Test - Things to Know
Developer Test - Things to KnowDeveloper Test - Things to Know
Developer Test - Things to Know
 
Agile Android
Agile AndroidAgile Android
Agile Android
 
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...
 
#codemotion2016: Everything you should know about testing to go with @pedro_g...
#codemotion2016: Everything you should know about testing to go with @pedro_g...#codemotion2016: Everything you should know about testing to go with @pedro_g...
#codemotion2016: Everything you should know about testing to go with @pedro_g...
 

Similaire à TDD CrashCourse Part4: Improving Testing

Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupDror Helper
 
How to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeHow to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeDaniel Wellman
 
Keep your Wicket application in production
Keep your Wicket application in productionKeep your Wicket application in production
Keep your Wicket application in productionMartijn Dashorst
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
Android testing
Android testingAndroid testing
Android testingSean Tsai
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEDarwin Durand
 
JDD2015: Where Test Doubles can lead you... - Sebastian Malaca
JDD2015: Where Test Doubles can lead you...  - Sebastian Malaca JDD2015: Where Test Doubles can lead you...  - Sebastian Malaca
JDD2015: Where Test Doubles can lead you... - Sebastian Malaca PROIDEA
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsTomek Kaczanowski
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8Dhaval Dalal
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphismUsama Malik
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6Fiyaz Hasan
 
Mocks introduction
Mocks introductionMocks introduction
Mocks introductionSperasoft
 
Testing in android
Testing in androidTesting in android
Testing in androidjtrindade
 

Similaire à TDD CrashCourse Part4: Improving Testing (20)

Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 
How to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy CodeHow to Start Test-Driven Development in Legacy Code
How to Start Test-Driven Development in Legacy Code
 
Keep your Wicket application in production
Keep your Wicket application in productionKeep your Wicket application in production
Keep your Wicket application in production
 
Writing Good Tests
Writing Good TestsWriting Good Tests
Writing Good Tests
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Android testing
Android testingAndroid testing
Android testing
 
Android TDD
Android TDDAndroid TDD
Android TDD
 
VISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLEVISUALIZAR REGISTROS EN UN JTABLE
VISUALIZAR REGISTROS EN UN JTABLE
 
Easy Button
Easy ButtonEasy Button
Easy Button
 
JDD2015: Where Test Doubles can lead you... - Sebastian Malaca
JDD2015: Where Test Doubles can lead you...  - Sebastian Malaca JDD2015: Where Test Doubles can lead you...  - Sebastian Malaca
JDD2015: Where Test Doubles can lead you... - Sebastian Malaca
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 
Inheritance and-polymorphism
Inheritance and-polymorphismInheritance and-polymorphism
Inheritance and-polymorphism
 
Test Engine
Test EngineTest Engine
Test Engine
 
What’s new in C# 6
What’s new in C# 6What’s new in C# 6
What’s new in C# 6
 
Test Engine
Test EngineTest Engine
Test Engine
 
Mocks introduction
Mocks introductionMocks introduction
Mocks introduction
 
Testing in android
Testing in androidTesting in android
Testing in android
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
SOLID Principles
SOLID PrinciplesSOLID Principles
SOLID Principles
 

Plus de David Rodenas

TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDDavid Rodenas
 
TDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingTDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingDavid Rodenas
 
Be professional: We Rule the World
Be professional: We Rule the WorldBe professional: We Rule the World
Be professional: We Rule the WorldDavid Rodenas
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorDavid Rodenas
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataDavid Rodenas
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214David Rodenas
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS ProgrammersDavid Rodenas
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersBasic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersDavid Rodenas
 
From high school to university and work
From high school to university and workFrom high school to university and work
From high school to university and workDavid Rodenas
 
Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1David Rodenas
 
Freelance i Enginyeria
Freelance i EnginyeriaFreelance i Enginyeria
Freelance i EnginyeriaDavid Rodenas
 
Angular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsAngular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsDavid Rodenas
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgottenDavid Rodenas
 
Testing: ¿what, how, why?
Testing: ¿what, how, why?Testing: ¿what, how, why?
Testing: ¿what, how, why?David Rodenas
 

Plus de David Rodenas (18)

TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDD
 
TDD CrashCourse Part1: Testing
TDD CrashCourse Part1: TestingTDD CrashCourse Part1: Testing
TDD CrashCourse Part1: Testing
 
Be professional: We Rule the World
Be professional: We Rule the WorldBe professional: We Rule the World
Be professional: We Rule the World
 
ES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD CalculatorES3-2020-P3 TDD Calculator
ES3-2020-P3 TDD Calculator
 
ES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game KataES3-2020-P2 Bowling Game Kata
ES3-2020-P2 Bowling Game Kata
 
ES3-2020-05 Testing
ES3-2020-05 TestingES3-2020-05 Testing
ES3-2020-05 Testing
 
Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214Testing, Learning and Professionalism — 20171214
Testing, Learning and Professionalism — 20171214
 
Vespres
VespresVespres
Vespres
 
Faster web pages
Faster web pagesFaster web pages
Faster web pages
 
Redux for ReactJS Programmers
Redux for ReactJS ProgrammersRedux for ReactJS Programmers
Redux for ReactJS Programmers
 
Basic Tutorial of React for Programmers
Basic Tutorial of React for ProgrammersBasic Tutorial of React for Programmers
Basic Tutorial of React for Programmers
 
From high school to university and work
From high school to university and workFrom high school to university and work
From high school to university and work
 
Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1Modules in angular 2.0 beta.1
Modules in angular 2.0 beta.1
 
Freelance i Enginyeria
Freelance i EnginyeriaFreelance i Enginyeria
Freelance i Enginyeria
 
Angular 1.X Community and API Decissions
Angular 1.X Community and API DecissionsAngular 1.X Community and API Decissions
Angular 1.X Community and API Decissions
 
MVS: An angular MVC
MVS: An angular MVCMVS: An angular MVC
MVS: An angular MVC
 
Mvc - Model: the great forgotten
Mvc - Model: the great forgottenMvc - Model: the great forgotten
Mvc - Model: the great forgotten
 
Testing: ¿what, how, why?
Testing: ¿what, how, why?Testing: ¿what, how, why?
Testing: ¿what, how, why?
 

Dernier

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
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
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
 
%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
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%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
 
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
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
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
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
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
 
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
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 

Dernier (20)

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
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
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
 
%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
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%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
 
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...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
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...
 
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...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
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
 
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
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 

TDD CrashCourse Part4: Improving Testing

  • 1. 05 BARCELONA David Rodenas, PhD TDD Course IMPROVE TESTING
  • 3. @drpicox @drpicox public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } HELLO WORLD 3 IMPROVE TESTING — HELLO WORLD
  • 4. @drpicox @drpicox public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } @Test public void say_hello_world() { HelloWorld.main(null); assertThat(???) } HELLO WORLD 4 IMPROVE TESTING — HELLO WORLD
  • 5. @drpicox @drpicox public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } @Test public void say_hello_world() { var buffer = new ByteArrayOutputStream(); var out = new PrintStream(buffer); System.setOut(out); HelloWorld.main(null); assertThat(buffer.toString()).isEqualTo("Hello Worldn"); } HELLO WORLD 5 IMPROVE TESTING — HELLO WORLD
  • 6. @drpicox @drpicox public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } @Test public void say_hello_world() { var realOut = System.out; var buffer = new ByteArrayOutputStream(); var testOut = new PrintStream(buffer); System.setOut(testOut); HelloWorld.main(null); System.setOut(realOut); assertThat(buffer.toString()).isEqualTo("Hello Worldn"); } HELLO WORLD 6 IMPROVE TESTING — HELLO WORLD
  • 7. @drpicox @drpicox public class HelloWorld { private PrintStream out; public HelloWorld(PrintStream out) { this.out = out; } public void sayHello() { this.out.println("Hello World"); } public static void main(String[] args) { new HelloWorld(System.out).sayHello(); } } HELLO WORLD 7 IMPROVE TESTING — HELLO WORLD
  • 8. @drpicox @drpicox @Test public void say_hello_world() { var buffer = new ByteArrayOutputStream(); var testOut = new PrintStream(buffer); new HelloWorld(testOut).sayHello(); assertThat(buffer.toString()).isEqualTo("Hello Worldn"); } HELLO WORLD 8 IMPROVE TESTING — HELLO WORLD
  • 9. @drpicox @drpicox HELLO WORLD 9 IMPROVE TESTING — HELLO WORLD public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } @Test public void say_hello_world() { var buffer = new ByteArrayOutputStream(); var out = new PrintStream(buffer); System.setOut(out); HelloWorld.main(null); assertThat(buffer.toString()).isEqualTo("Hello Worldn"); }
  • 10. @drpicox @drpicox @Test public void say_hello_world() { var buffer = new ByteArrayOutputStream(); var testOut = new PrintStream(buffer); new HelloWorld(testOut).sayHello(); assertThat(buffer.toString()).isEqualTo("Hello Worldn"); } HELLO WORLD 10 IMPROVE TESTING — HELLO WORLD
  • 12. 12
  • 13. @drpicox @drpicox Create one single instance, do not use static LOWER "S" SINGLETON 13 IMPROVE TESTING — PATTERNS // wrong public class House { private static House instance; public static House getInstance() { if (instance == null) instance = new House(); return instance; } } // correct var house = new House();
  • 14. @drpicox @drpicox Ask for what you operate; do not look for things LAW OF DEMETER 14 IMPROVE TESTING — PATTERNS // wrong house.getDoor().open() // correct door.open();
  • 15. @drpicox @drpicox Give me what I need DEPENDENCY INJECTION 15 IMPROVE TESTING — PATTERNS // wrong (Java) public class Clicker { public void click() { House.getInstance().getDoor().open(); } } // wrong (Javascript) import house from './house' export class Clicker { click() { house.getDoor().open(); } }
  • 16. @drpicox @drpicox Give me what I need DEPENDENCY INJECTION 16 IMPROVE TESTING — PATTERNS // correct public class Clicker { public void click(Door door) { door.open(); } }
  • 17. @drpicox @drpicox Give me what I need DEPENDENCY INJECTION 17 IMPROVE TESTING — PATTERNS // better (do not breaks method signature) public class Clicker { private Door door; public void Clicker(Door door) { this.door = door; } public void click() { door.open(); } }
  • 19. @drpicox @drpicox Deceptive API STATICS ARE LIARS 19 IMPROVE TESTING — STATICS ARE LIARS testCharge() { var cc = new CreditCard("123...234") cc.charge(100); } Throws an exception... why?
  • 20. @drpicox @drpicox Deceptive API STATICS ARE LIARS 20 IMPROVE TESTING — STATICS ARE LIARS testCharge() { CreditCardProcessor.init(...); var cc = new CreditCard("123...234") cc.charge(100); } Throws an exception... why?
  • 21. @drpicox @drpicox Deceptive API STATICS ARE LIARS 21 IMPROVE TESTING — STATICS ARE LIARS testCharge() { OfflineQueue.start(); CreditCardProcessor.init(...); var cc = new CreditCard("123...234") cc.charge(100); } Throws an exception... why?
  • 22. @drpicox @drpicox Deceptive API STATICS ARE LIARS 22 IMPROVE TESTING — STATICS ARE LIARS testCharge() { Database.connect(...); OfflineQueue.start(); CreditCardProcessor.init(...); var cc = new CreditCard("123...234") cc.charge(100); } Now works... and makes the charge...
  • 23. @drpicox @drpicox Deceptive API STATICS ARE LIARS 23 IMPROVE TESTING — STATICS ARE LIARS testCharge() { var db = Database(); var q = new OfflineQueue(db); var ccp = new CreditCardProcessor(q); var cc = new CreditCard("123...234") cc.charge(100, ccp); } Now works... and makes the charge...
  • 24. @drpicox @drpicox Deceptive API STATICS ARE LIARS 24 IMPROVE TESTING — STATICS ARE LIARS testCharge() { var db = MockDatabase(); var q = new OfflineQueue(db); var ccp = new CreditCardProcessor(q); var cc = new CreditCard("123...234") cc.charge(100, ccp); assertThat(db).hasCharge(100, cc); }
  • 26. @drpicox @drpicox public class House { Door door; Window window; Roof roof; Kitchen kitchen; LivingRoom livingRoom; BedRoom bedRoom; House(Door door, Window window, Roof roof, Kitchen kitchen, LivingRoom livingRoom, BedRoom bedRoom) { this.door = Assert.notNull(door); this.window = Assert.notNull(window); this.roof = Assert.notNull(roof); this.kitchen = Assert.notNull(kitchen); this.livingRoom = Assert.notNull(livingRoom); this.bedRoom = Assert.notNull(bedRoom); } void secure() { door.lock(); window.close(); } } 26 IMPROVE TESTING — AVOID CODE ASSERTIONS
  • 27. @drpicox @drpicox public class TestHouse { @Test testSecureHouse() { Door door = new Door(); Window window = new Window(); House house = new House(door, window, new Roof(), new Kitchen(), new LivingRoom(), new BedRoom()); house.secure(); assertTrue(door.isLocked()); assertTrue(window.isClosed()); } } 27 IMPROVE TESTING — AVOID CODE ASSERTIONS Irrelevant instances on the test (what if they had also complex constructor?) 😱
  • 28. @drpicox @drpicox public class TestHouse { @Test testSecureHouse() { Door door = new Door(); Window window = new Window(); House house = new House(door, window, null, null, null, null); house.secure(); assertTrue(door.isLocked()); assertTrue(window.isClosed()); } } 28 IMPROVE TESTING — AVOID CODE ASSERTIONS We do not care, faster and easier to understand
  • 30. @drpicox @drpicox public class TemperatureLogger implements Logger { public void log(Notebook notebook) { var weatherStation = WeatherStation.getInstance(); var thermometer = weatherStation.getThermometer(); var temperature = thermometer.getTemperature(); notebook.annotate(temperature); } } // It is instantiated like this: var logger = new TemperatureLogger(); TEMPERATURE LOGGER 30 IMPROVE TESTING — FIXING EXAMPLES
  • 31. @drpicox @drpicox public class TemperatureLogger implements Logger { private WeatherStation weatherStation; TemperatureLogger(WeatherStation weatherStation) { this.weatherStation = weatherStation; } public void log(Notebook notebook) { var thermometer = weatherStation.getThermometer(); var temperature = thermometer.getTemperature(); notebook.annotate(temperature); } } // It is instantiated like this: var logger = new TemperatureLogger(weatherStation); TEMPERATURE LOGGER 31 IMPROVE TESTING — FIXING EXAMPLES
  • 32. @drpicox @drpicox public class TemperatureLogger implements Logger { private Thermometer thermometer; TemperatureLogger(Thermometer thermometer) { this.thermometer = thermometer; } public void log(Notebook notebook) { var temperature = thermometer.getTemperature(); notebook.annotate(temperature); } } // It is instantiated like this: var logger = new TemperatureLogger( weatherStation.getThermometer() ); TEMPERATURE LOGGER 32 IMPROVE TESTING — FIXING EXAMPLES
  • 33. @drpicox @drpicox public class WheelTester { private LegalRules legalRules; public WheelTester(LegalRules legalRules) { this.legalRules = legalRules; } public boolean checkWheels(Car car) { Thikness thikness = legalRules.getThikness(); List<Wheel> wheels = car.getWheels(); for (Wheel wheel: wheels) { boolean isOk = wheel.hasThikness(thikness); if (!isOk) return false; } return true; } } WHEEL TESTER 33 IMPROVE TESTING — FIXING EXAMPLES
  • 34. @drpicox @drpicox public class WheelTester { private Thikness thikness; public WheelTester(Thikness thikness) { this.thikness = thikness; } public boolean checkWheels(Car car) { List<Wheel> wheels = car.getWheels(); for (Wheel wheel: wheels) { boolean isOk = wheel.hasThikness(thikness); if (!isOk) return false; } return true; } } WHEEL TESTER 34 IMPROVE TESTING — FIXING EXAMPLES
  • 35. @drpicox @drpicox public class WheelTester { private Thikness thikness; public WheelTester(Thikness thikness) { this.thikness = thikness; } public boolean checkWheels(List<Wheel> wheels) { for (Wheel wheel: wheels) { boolean isOk = wheel.hasThikness(thikness); if (!isOk) return false; } return true; } } WHEEL TESTER 35 IMPROVE TESTING — FIXING EXAMPLES
  • 36. @drpicox @drpicox public class HDFormatterA { private HD hd; public HDFormatterA() { hd = new HardDisk(); } public void doWork() { hd.open(); hd.format("FAT"); hd.close(); } } HD FORMATTER 36 IMPROVE TESTING — FIXING EXAMPLES
  • 37. @drpicox @drpicox public class HDFormatterB { private HD hd; public HDFormatterB() { hd = HardDisk.getInstance(); } public void doWork() { hd.open(); hd.format("FAT"); hd.close(); } } HD FORMATTER 37 IMPROVE TESTING — FIXING EXAMPLES
  • 38. @drpicox @drpicox public class HDFormatterC { private HD hd; public HDFormatterC() { hd = ServiceLocator.get(HD.class); } public void doWork() { hd.open(); hd.format("FAT"); hd.close(); } } HD FORMATTER 38 IMPROVE TESTING — FIXING EXAMPLES public void test_hdformatterc() { var hd = new MockHD(); ServiceLocator.set(HD.class, hd); new HDFormatterC().doWork(); assertThat(hd.verify()).isTrue(); }
  • 39. @drpicox @drpicox public class HDFormatterD { private HD hd; public HDFormatterD(HD hd) { this.hd = hd; } public void doWork() { hd.open(); hd.format("FAT"); hd.close(); } } HD FORMATTER 39 IMPROVE TESTING — FIXING EXAMPLES public void test_hdformatterd() { var hd = new MockHD(); new HDFormatterD().doWork(hd); assertThat(hd.verify()).isTrue(); }