SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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
 

Was ist angesagt? (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...
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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?
 

Kürzlich hochgeladen

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 

Kürzlich hochgeladen (20)

Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 

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(); }