SlideShare una empresa de Scribd logo
1 de 52
Descargar para leer sin conexión
Unit Testing with
JUnit Boot Camp
Peter Kofler, ‘Code Cop’
@codecopkofler
www.code-cop.org
Copyright Peter Kofler, licensed under CC-BY.
Peter Kofler
• Ph.D. (Appl. Math.)
• Professional Software
Developer for 15+ years
• “fanatic about code quality”
• I help development teams
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
I help development teams with
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Professionalism
●
Quality and
Productivity
●
Continuous
Improvement
Mentoring
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
●
Pair Programming
●
Programming
Workshops
●
Deliberate
Practice, e.g.
Coding Dojos
Developing Quality
Software Developers
Agenda
●
JUnit
●
Coding Exercise
●
Unit Tests
●
Lunch Break
●
Clean Unit Tests
●
Coding Exercise
●
Retrospective
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
How to take advantage
●
Slow down
●
Focus on doing it right
●
Get out of your comfort zone
●
Embrace freedom of experimenting
●
Pair with strangers you do not know
●
What you learn is your responsibility
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Introduce Yourself
●
Your Name
●
Who has heard of JUnit? (xUnit)
●
Who has never written a unit test?
●
Who has written
a few?
●
Who writes unit
tests every day?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
JUnit
JUnit
●
http://junit.org/
●
a (unit) testing framework
●
tests are executed within a framework
●
no output from your tests
●
check expectations programmatically
●
test runner reports failure on each test
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Project – Class - Method
●
Maven
●
src/test/java
●
Test Class
public class SomeTestClass
{
@Test
public void someTestMethod()
{
...
}
}
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
JUnit Assertions
import static org.junit.Assert.*;
assertEquals("SNEK", actual);
assertTrue(isSaved);
assertNotNull(customer);
assertArrayEquals(
new String[] {"Berg", "Wien"},
citiesArray);
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Hamcrest Assertions
import static org.junit.Assert.assertThat;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.hamcrest.core.StringContains.containsString;
import static org.hamcrest.core.StringEndsWith.endsWith;
assertThat(actual,instanceOf(Customer.class));
assertThat(message,
containsString("abgelaufen"));
assertThat(message, endsWith("des Jahres."));
assertThat(citiesCollection, hasItem("Wien"));
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Try it yourself
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Setup
●
Find a pair.
●
Get the code
(https://bitbucket.org/pkofler/junit-koans)
●
Run JUnit - should see no tests
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Pair Programming
●
Collaborative = Pair Programming
●
do not talk for
too long
●
do not interrupt
the other
●
no “keyboard
hugging“
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Pair Programming
adds discussion &
a second opinion
to the practice.
Assignment
●
Go through the test code
●
Assertions are commented/ incomplete
●
uncomment the assertions
●
and complete them making tests pass
●
Explore features of JUnit
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
→Practice
Keep the bar green to
keep the code clean
Unit Tests
Why write tests?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
“I write unit tests for one
reason: so my coworkers
don't f*** up my code.”
(David Angry)
Unit Test (Micro Test)
●
code written by a developer
●
tests an individual unit
●
isolate each part
●
shows that the individual part is correct
●
sort of living documentation
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Unit of Work
●
single logical functional use case
●
invoked by some public interface
●
a single method,
●
a whole class or
●
multiple classes
●
with one single logical purpose
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Focus on Behaviour

e.g. requirements are behaviour

names from (problem) domain

full sentences
(long, descriptive test method names)

expected behaviour should

separation per business module
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Consist of 3 Simple Steps
●
Prepare Input – Arrange
●
Call Method – Act
●
Check Output – Assert
●
Use in form of Given-When-Then
●
No conditional logic ( more test cases)→
●
No loops ( parametrized test)→
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Only one aspect/feature
●
tests work best with lots of small tests
●
tests only fail because of one reason
●
more than 1 assert in test
split into multiple tests→
●
1 test case – 1 method – 1 assertion
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
testHighAndLowInput()
testHighInput()
testLowInput()
Attributes of a
Good Unit Test?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
F.I.R.S.T
●
Fast
●
Isolated (data, sequence)
●
Repeatable
●
Self-verifying
●
Timely
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Clean, Readable
and Expressive?
http://theprofoundprogrammer.com/post/26561881517/text-single-letter-variables-who-the-fuck-do
Clean?
●
Free from dirt or marks:
e.g. a clean kitchen floor.
●
Without imperfections or errors:
e.g. a clean edge.
●
What if all your tests would be nicely
structured and consistent?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Readable?
●
Easily read; legible:
e.g. a readable typeface.
●
Enjoyable or interesting to read:
e.g. a readable story.
●
What if a test suite would be a readable
document at the same time?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Expressive?
●
Full of expression; meaningful:
e.g. an expressive shrug.
●
Effectively conveying thought:
e.g. an expressive glance.
●
What if tests revealed their intend?
Would express what should happen?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Clean Tests
●
Are of same quality as production code.
●
Are clean code, structured, consistent.
●
Are a readable document.
●
Reveal their intend and express what
should happen.
●
Give informative error message on
failure.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Welcome to the Gilded Rose
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
The existing inventory system
●
We have items to sell. Items degrade in
quality the older they get.
●
All items have a SellIn value which
denotes the number of days we have to
sell the item.
●
All items have a Quality value which
denotes how valuable the item is.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Requirements
●
At the end of each day our system lowers
both values for every item.
●
Once the sell by date has passed, Quality
degrades twice as fast.
●
The Quality of an item is never negative.
●
The Quality is never more than 50.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Special Item: Brie
●
Aged Brie actually increases in Quality
the older it gets.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Backstage Passes
●
A backstage pass
increases in Quality
as it's SellIn value
approaches (by a
complex formula)
●
but Quality drops to
0 after the concert.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Special Item
●
Sulfuras, a legendary item, never has to
be sold or decreases in Quality.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Setup
● Find a pair.
● Get the code.
(https://github.com/emilybache/GildedRose-Refactoring-Kata)
● Run tests, should see single failing test.
● Read GildedRoseRequirements.txt
● Run TextTestFixture to see what it does.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Assignment
●
Create “perfect” unit tests
●
derive test cases from requirements
●
cover all cases e.g. boundary conditions
●
readable, concise, free of duplication
●
Experiment with different styles.
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Create a test suite
that is a readable
document at the
same time!
Don't Focus on
Getting it Done.
F0cus on Doing
It Perfectly.
→Practice
Closing Circle
●
What did you learn today?
●
What surprised you today?
●
What will you do
differently in the
future?
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
Peter Kofler
@codecopkofler
www.code-cop.org
Kata by
Emily Bache
@emilybache
http://coding-is-like-cooking.info/2013/03/writing-good-tests-for-the-gilded-rose-kata/
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
CC Images
● Bruce http://www.flickr.com/photos/sherpas428/4350620602/
● pairing http://www.flickr.com/photos/dav/94735395/
● agenda http://www.flickr.com/photos/24293932@N00/2752221871/
● wants you http://www.flickr.com/photos/shutter/105497713/
● hands https://www.flickr.com/photos/ninahiironniemi/497993647/
● green http://www.flickr.com/photos/43442082@N00/296362882/
● inn http://www.flickr.com/photos/danielleblue/170496395/
● Brie http://www.flickr.com/photos/chez_loulou/2767503201
● pass http://www.flickr.com/photos/frf_kmeron/5556518514
● Sulfuras https://www.flickr.com/photos/sharelabs/11195626116
PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY

Más contenido relacionado

La actualidad más candente

Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Peter Kofler
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Peter Kofler
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)Peter Kofler
 
Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Peter Kofler
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Peter Kofler
 
TDD as if You Meant It (2013)
TDD as if You Meant It (2013)TDD as if You Meant It (2013)
TDD as if You Meant It (2013)Peter Kofler
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Peter Kofler
 
Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)Peter Kofler
 
Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)Peter Kofler
 
Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Peter Kofler
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Peter Kofler
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Peter Kofler
 
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Peter Kofler
 
Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)Peter Kofler
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Peter Kofler
 
Outside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDOutside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDPeter Kofler
 
Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)Peter Kofler
 
Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)Peter Kofler
 
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Peter Kofler
 

La actualidad más candente (20)

Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)Coding Dojo: Functional Calisthenics (2016)
Coding Dojo: Functional Calisthenics (2016)
 
Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)Brutal Coding Constraints (ITAKE 2017)
Brutal Coding Constraints (ITAKE 2017)
 
The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)The Brutal Refactoring Game (2013)
The Brutal Refactoring Game (2013)
 
Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013Code Retreat Graz, Austria 2013
Code Retreat Graz, Austria 2013
 
Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)Extract Method Refactoring Workshop (2016)
Extract Method Refactoring Workshop (2016)
 
Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)Designing Test Cases for the Gilded Rose Kata (2013)
Designing Test Cases for the Gilded Rose Kata (2013)
 
TDD as if You Meant It (2013)
TDD as if You Meant It (2013)TDD as if You Meant It (2013)
TDD as if You Meant It (2013)
 
Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)Coding Dojo Object Calisthenics (2016)
Coding Dojo Object Calisthenics (2016)
 
Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)Coding Dojo: Baby Steps Push Challenge (2021)
Coding Dojo: Baby Steps Push Challenge (2021)
 
Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)Coding Dojo: Bank OCR Outside-In (2015)
Coding Dojo: Bank OCR Outside-In (2015)
 
Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)Deliberate Practice, New Learning Styles (2015)
Deliberate Practice, New Learning Styles (2015)
 
Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)Software Craftsmanship Journeyman Tour (2013)
Software Craftsmanship Journeyman Tour (2013)
 
Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)Coding Dojo: Mars Rover (2014)
Coding Dojo: Mars Rover (2014)
 
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
Using Automated Code Reviews to Achieve Continuous Quality (ASQF Agile Night ...
 
Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)Coding Dojo: Naming with Dices (2021)
Coding Dojo: Naming with Dices (2021)
 
Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)Idiomatic R for Rosetta Code (2013)
Idiomatic R for Rosetta Code (2013)
 
Outside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDDOutside-in Test Driven Development - the London School of TDD
Outside-in Test Driven Development - the London School of TDD
 
Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)Coding Dojo: Adding Tests to Legacy Code (2014)
Coding Dojo: Adding Tests to Legacy Code (2014)
 
Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)Refactoring the Tennis Kata (2013)
Refactoring the Tennis Kata (2013)
 
Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)Designing Test Cases for the Gilded Rose Kata v2 (2015)
Designing Test Cases for the Gilded Rose Kata v2 (2015)
 

Similar a JUnit Boot Camp (GeeCON 2016)

Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Peter Kofler
 
Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Peter Kofler
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)Peter Kofler
 
Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)Peter Kofler
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Peter Kofler
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Peter Kofler
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Peter Kofler
 
Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)Peter Kofler
 
Deliberate Practice (2014)
Deliberate Practice (2014)Deliberate Practice (2014)
Deliberate Practice (2014)Peter Kofler
 
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Peter Kofler
 
Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Peter Kofler
 
Pair Programming (2014)
Pair Programming (2014)Pair Programming (2014)
Pair Programming (2014)Peter Kofler
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Peter Kofler
 
Software testing and quality assurance
Software testing and quality assuranceSoftware testing and quality assurance
Software testing and quality assuranceBenjamin Baumann
 
Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Peter Kofler
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Danny Preussler
 
Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Peter Kofler
 

Similar a JUnit Boot Camp (GeeCON 2016) (17)

Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)Pragmatic Introduction to Python Unit Testing (PyDays 2018)
Pragmatic Introduction to Python Unit Testing (PyDays 2018)
 
Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)Prime Factors Code Kata - Practicing TDD (2014)
Prime Factors Code Kata - Practicing TDD (2014)
 
Pair Programming (2015)
Pair Programming (2015)Pair Programming (2015)
Pair Programming (2015)
 
Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)Coding Dojo: Baby Steps (2014)
Coding Dojo: Baby Steps (2014)
 
Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)Code Quality Assurance v4 (2013)
Code Quality Assurance v4 (2013)
 
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
Coding Dojo for Testers/Testing Dojo: Designing Test Cases with FitNesse (2014)
 
Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)Pragmatic Introduction to PHP Unit Testing (2015)
Pragmatic Introduction to PHP Unit Testing (2015)
 
Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)Coding Dojo: Roman Numerals (2014)
Coding Dojo: Roman Numerals (2014)
 
Deliberate Practice (2014)
Deliberate Practice (2014)Deliberate Practice (2014)
Deliberate Practice (2014)
 
Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)Coding Dojo: Fun with Tic-Tac-Toe (2014)
Coding Dojo: Fun with Tic-Tac-Toe (2014)
 
Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)Coding Dojo: String Calculator (2013)
Coding Dojo: String Calculator (2013)
 
Pair Programming (2014)
Pair Programming (2014)Pair Programming (2014)
Pair Programming (2014)
 
Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)Coding Dojo: Bank OCR (2014)
Coding Dojo: Bank OCR (2014)
 
Software testing and quality assurance
Software testing and quality assuranceSoftware testing and quality assurance
Software testing and quality assurance
 
Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)Can PL/SQL be Clean? (2013)
Can PL/SQL be Clean? (2013)
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)Practical (J)Unit Testing (2009)
Practical (J)Unit Testing (2009)
 

Último

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 

Último (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 

JUnit Boot Camp (GeeCON 2016)

  • 1. Unit Testing with JUnit Boot Camp Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.
  • 2. Peter Kofler • Ph.D. (Appl. Math.) • Professional Software Developer for 15+ years • “fanatic about code quality” • I help development teams PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 3. I help development teams with PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Professionalism ● Quality and Productivity ● Continuous Improvement
  • 4. Mentoring PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY ● Pair Programming ● Programming Workshops ● Deliberate Practice, e.g. Coding Dojos
  • 6. Agenda ● JUnit ● Coding Exercise ● Unit Tests ● Lunch Break ● Clean Unit Tests ● Coding Exercise ● Retrospective PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 7. How to take advantage ● Slow down ● Focus on doing it right ● Get out of your comfort zone ● Embrace freedom of experimenting ● Pair with strangers you do not know ● What you learn is your responsibility PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 8. Introduce Yourself ● Your Name ● Who has heard of JUnit? (xUnit) ● Who has never written a unit test? ● Who has written a few? ● Who writes unit tests every day? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 10. JUnit ● http://junit.org/ ● a (unit) testing framework ● tests are executed within a framework ● no output from your tests ● check expectations programmatically ● test runner reports failure on each test PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 11. Project – Class - Method ● Maven ● src/test/java ● Test Class public class SomeTestClass { @Test public void someTestMethod() { ... } } PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 12. JUnit Assertions import static org.junit.Assert.*; assertEquals("SNEK", actual); assertTrue(isSaved); assertNotNull(customer); assertArrayEquals( new String[] {"Berg", "Wien"}, citiesArray); PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 13. Hamcrest Assertions import static org.junit.Assert.assertThat; import static org.hamcrest.core.IsCollectionContaining.hasItem; import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.hamcrest.core.StringContains.containsString; import static org.hamcrest.core.StringEndsWith.endsWith; assertThat(actual,instanceOf(Customer.class)); assertThat(message, containsString("abgelaufen")); assertThat(message, endsWith("des Jahres.")); assertThat(citiesCollection, hasItem("Wien")); PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 14. Try it yourself PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 15. Setup ● Find a pair. ● Get the code (https://bitbucket.org/pkofler/junit-koans) ● Run JUnit - should see no tests PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 16. Pair Programming ● Collaborative = Pair Programming ● do not talk for too long ● do not interrupt the other ● no “keyboard hugging“ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 17. Pair Programming adds discussion & a second opinion to the practice.
  • 18. Assignment ● Go through the test code ● Assertions are commented/ incomplete ● uncomment the assertions ● and complete them making tests pass ● Explore features of JUnit PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 20.
  • 21. Keep the bar green to keep the code clean
  • 23. Why write tests? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 24. “I write unit tests for one reason: so my coworkers don't f*** up my code.” (David Angry)
  • 25. Unit Test (Micro Test) ● code written by a developer ● tests an individual unit ● isolate each part ● shows that the individual part is correct ● sort of living documentation PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 26. Unit of Work ● single logical functional use case ● invoked by some public interface ● a single method, ● a whole class or ● multiple classes ● with one single logical purpose PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 27. Focus on Behaviour  e.g. requirements are behaviour  names from (problem) domain  full sentences (long, descriptive test method names)  expected behaviour should  separation per business module PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 28. Consist of 3 Simple Steps ● Prepare Input – Arrange ● Call Method – Act ● Check Output – Assert ● Use in form of Given-When-Then ● No conditional logic ( more test cases)→ ● No loops ( parametrized test)→ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 29. Only one aspect/feature ● tests work best with lots of small tests ● tests only fail because of one reason ● more than 1 assert in test split into multiple tests→ ● 1 test case – 1 method – 1 assertion PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY testHighAndLowInput() testHighInput() testLowInput()
  • 30. Attributes of a Good Unit Test? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 34. Clean? ● Free from dirt or marks: e.g. a clean kitchen floor. ● Without imperfections or errors: e.g. a clean edge. ● What if all your tests would be nicely structured and consistent? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 35. Readable? ● Easily read; legible: e.g. a readable typeface. ● Enjoyable or interesting to read: e.g. a readable story. ● What if a test suite would be a readable document at the same time? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 36. Expressive? ● Full of expression; meaningful: e.g. an expressive shrug. ● Effectively conveying thought: e.g. an expressive glance. ● What if tests revealed their intend? Would express what should happen? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 37. Clean Tests ● Are of same quality as production code. ● Are clean code, structured, consistent. ● Are a readable document. ● Reveal their intend and express what should happen. ● Give informative error message on failure. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 38. Welcome to the Gilded Rose PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 39. The existing inventory system ● We have items to sell. Items degrade in quality the older they get. ● All items have a SellIn value which denotes the number of days we have to sell the item. ● All items have a Quality value which denotes how valuable the item is. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 40. Requirements ● At the end of each day our system lowers both values for every item. ● Once the sell by date has passed, Quality degrades twice as fast. ● The Quality of an item is never negative. ● The Quality is never more than 50. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 41. Special Item: Brie ● Aged Brie actually increases in Quality the older it gets. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 42. Backstage Passes ● A backstage pass increases in Quality as it's SellIn value approaches (by a complex formula) ● but Quality drops to 0 after the concert. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 43. Special Item ● Sulfuras, a legendary item, never has to be sold or decreases in Quality. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 44. Setup ● Find a pair. ● Get the code. (https://github.com/emilybache/GildedRose-Refactoring-Kata) ● Run tests, should see single failing test. ● Read GildedRoseRequirements.txt ● Run TextTestFixture to see what it does. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 45. Assignment ● Create “perfect” unit tests ● derive test cases from requirements ● cover all cases e.g. boundary conditions ● readable, concise, free of duplication ● Experiment with different styles. PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 46. Create a test suite that is a readable document at the same time!
  • 47. Don't Focus on Getting it Done. F0cus on Doing It Perfectly.
  • 49.
  • 50. Closing Circle ● What did you learn today? ● What surprised you today? ● What will you do differently in the future? PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 51. Peter Kofler @codecopkofler www.code-cop.org Kata by Emily Bache @emilybache http://coding-is-like-cooking.info/2013/03/writing-good-tests-for-the-gilded-rose-kata/ PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY
  • 52. CC Images ● Bruce http://www.flickr.com/photos/sherpas428/4350620602/ ● pairing http://www.flickr.com/photos/dav/94735395/ ● agenda http://www.flickr.com/photos/24293932@N00/2752221871/ ● wants you http://www.flickr.com/photos/shutter/105497713/ ● hands https://www.flickr.com/photos/ninahiironniemi/497993647/ ● green http://www.flickr.com/photos/43442082@N00/296362882/ ● inn http://www.flickr.com/photos/danielleblue/170496395/ ● Brie http://www.flickr.com/photos/chez_loulou/2767503201 ● pass http://www.flickr.com/photos/frf_kmeron/5556518514 ● Sulfuras https://www.flickr.com/photos/sharelabs/11195626116 PETER KOFLER, CODE-COP.ORG FANATIC ABOUT CODE QUALITY