SlideShare a Scribd company logo
Java 201 โ€“ Intro to Test-Driven
Development in Java
Agenda
โ€ข TDD Overview
โ€ข Test Frameworks
โ€ข Setting Up your IDE
โ€ข Writing your first Unit Test
โ€ข Anatomy of a Unit Test
โ€ข Hands-on Exercise
Intro to Test-Driven Development
in Java
TDD Overview
TDD Overview
โ€ข What is TDD
โ€ข Why TDD
โ€ข Feedback Loop
โ€ข TDD Workflow
What is TDD?
โ€ข Technique for specifying system features with
tests
โ€ข Tests are written before production code
โ€ข Simple, repeated, short-cycled mechanism
โ€ข Automated mechanism for regression testing
of code changes
Why TDD?
โ€ข Improves code quality
โ€ข Minimises defects
โ€ข Increases code maintainability
โ€ข Reduces the cost of change
โ€ข Provides a quick feedback
โ€ข Acts as documentation for your code
โ€ข Reduces fear of breaking things
โ€ข It is fun and infectious ๏Š
Feedback Loop
PDCA Explained
โ€ข PLAN
โ€“ Establish the objectives
โ€“ Identify steps required to meet the objective
โ€ข DO
โ€“ Execute the steps to meet the objective
โ€ข CHECK
โ€“ Study actual results and compare against expected results
โ€“ Look for completeness in meeting the objectives
โ€ข ACT
โ€“ If CHECK step shows objectives met vs. with the plan,
repeat with next objective else rework and CHECK again
TDD Workflow
Intro to Test-Driven Development
in Java
Test Frameworks
What is a Test Framework?
โ€ข An abstract set ofโ€ฆ
โ€“ concepts, processes, procedures, environments
โ€ข Where automated tests areโ€ฆ
โ€“ designed, implemented and executed
โ€ข Includes physical structures forโ€ฆ
โ€“ test creation
โ€“ test execution
โ€ข As well asโ€ฆ
โ€“ logical interactions amongst components
Why Test Frameworks?
โ€ข Provides the basis of test automation
โ€ข Simplifies test automation efforts
โ€ข Provides concepts and tools that support
automated testing
Common Test Frameworks
โ€ข Code Testing
โ€“ Junit (http://www.junit.org)
โ€“ TestNG (http://testng.org)
โ€ข UI Testing
โ€“ Selenium (www.selenium.org)
โ€“ Appium (http://appium.io/)
โ€ข API Testing
โ€“ SoapUI (http://www.soapui.org)
โ€“ RestAssured (https://code.google.com/p/rest-assured)
Intro to Test-Driven Development
in Java
Setting Up your IDE
Setting Up
โ€ข Required Software
โ€ข Download JUnit
โ€ข Create Java201 Project
โ€ข Add JUnit to the Classpath
โ€ข Create HelloGreetingTest
โ€ข Run the HelloGreetingTest
โ€ข Create the HelloGreeting class
โ€ข Re-run the HelloGreetingTest
Required Software
โ€ข IDE
โ€ข Java SDK
โ€“ http://www.oracle.com/technetwork/java/javase/
downloads/jdk8-downloads-2133151.html
โ€ข JUnit
โ€“ www.junit.org
Download JUnit
โ€ข Download the following files from
www.junit.org to a folder on your hard drive
โ€“ junit.jar
โ€“ hamcrest-core.jar
Create Java201 Project
โ€ข Create new project named Java201
โ€ข Create a new source folder named test in the
java201 project
โ€ข Create a lib folder in the Java201 project
โ€ข In the test source folder, create the
java201.greetings package
Add JUnit to the Classpath
โ€ข Copy the junit.jar and hamcrest-core.jar files to
the lib folder of your project
โ€ข Refresh the project to import the new contents
of the lib folder
โ€ข In Eclipse, click on the Java201 project, select
File>Properties>Build Path and click on
Libraries tab
โ€ข Click Add JARs, browse to the project lib folder,
select both jar files and click OK twice
Create HelloGreetingTest.java
โ€ข In Eclipse, Right-click the test folder, select New>JUnit Test Case,
enter HelloGreetingTest in the Name field and click Finish
HelloGreetingTest.java
Implement HelloGreetingTest.java
Run the HelloGreetingTest
โ€ข In Eclipse, Right-click the HelloGreetingTest and select
Run As>JUnit Test
Create HelloGreeting class
โ€ข In Eclipse, right-click the src folder and select New>Class, enter
HelloGreeting as class name and click Finish
โ€ข Create a sayHello() method that returns the String โ€œHello, TDD!โ€
Re-run the HelloGreetingTest
โ€ข Declare and initialise HelloGreeting object and
assert it returns โ€œHello, TDD!โ€ and rerun the test
Java 201   Intro to Test Driven Development in Java
Intro to Test-Driven Development
in Java
Anatomy of a Unit Test
Anatomy of a Unit Test
Declaration of
class under test
setUp() runs once
before each test method
A test method
Initialization of
class under test
tearDown() runs once
after each test method
Checking test results
Intro to Test-Driven Development
in Java
Hands-on Exercise
Exercise: Game of Cards
โ€ข Using TDD, create the following building blocks of a
typical card game
โ€“ Card
โ€ข Has a suite (Club, Heart, Diamond, Spade) and rank (2-10, Jack,
Queen, King, Ace)
โ€ข Ace is highest card
โ€“ Hand
โ€ข Consists of one or more Cards
โ€“ Deck
โ€ข Contains 52 Cards, 13 cards per suite
โ€“ Dealer
โ€ข Deals cards from a deck
โ€ข Each player will be dealt a Hand of Cards
Card
rank
suite
getRank() : Rank
setRank() : void
getSuite() : Suite
setSuite() : void
Hand
cards
maxCards
getNoOfCards() : int
drawCard(): Card
addCard() : void
Deck
cards
getCards(int noOFCards) :
List<Card>
Dealer
deck
deal(noOfCards) : Hand
Game of Cards Domain Model
Solution: Game of Cards
https://github.com/hawkmanacademy/java201
Resources
โ€ข UML Distilled - http://amzn.to/1BBHcXi
โ€ข Test Driven Development โ€“
http://amzn.to/1xqPllN
โ€ข XUnit Patterns - http://xunitpatterns.com/
โ€ข Junit โ€“ www.junit.org

More Related Content

What's hot (12)

PPT
Core java Basics
RAMU KOLLI
ย 
PPTX
6. static keyword
Indu Sharma Bhardwaj
ย 
PPTX
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
ย 
PPT
Object Oriented Programming with Java
Jussi Pohjolainen
ย 
PPTX
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
ย 
PPT
Core java concepts
Ram132
ย 
PPT
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
ย 
PDF
Basic java for Android Developer
Nattapong Tonprasert
ย 
PPTX
Scala, Play 2.0 & Cloud Foundry
Pray Desai
ย 
PPTX
Core java
Shivaraj R
ย 
PPTX
Introduction to Scala
Rahul Jain
ย 
PPTX
Core java complete ppt(note)
arvind pandey
ย 
Core java Basics
RAMU KOLLI
ย 
6. static keyword
Indu Sharma Bhardwaj
ย 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
ย 
Object Oriented Programming with Java
Jussi Pohjolainen
ย 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
ย 
Core java concepts
Ram132
ย 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Sagar Verma
ย 
Basic java for Android Developer
Nattapong Tonprasert
ย 
Scala, Play 2.0 & Cloud Foundry
Pray Desai
ย 
Core java
Shivaraj R
ย 
Introduction to Scala
Rahul Jain
ย 
Core java complete ppt(note)
arvind pandey
ย 

Viewers also liked (20)

PPTX
Java 102 intro to object-oriented programming in java - exercises
agorolabs
ย 
PDF
Introduction to Agile
agorolabs
ย 
PPTX
Computer Programming Overview
agorolabs
ย 
PPTX
Java 103 intro to java data structures
agorolabs
ย 
ODP
C'est si simple git ! (devoxx france 2012)
Ulrich VACHON
ย 
PPT
Test driven-development
David Paluy
ย 
PDF
Mix it 2016 - Software craftsmanship : le chainon manquant de lโ€™agilitรฉ ?
Nicolas Ruffel
ย 
PDF
TDD - Short for Test Driven Development!
Bradley Cypert
ย 
PDF
Prรฉsentation du versioning avec Git
msadouni
ย 
PPTX
Prรฉsentation de git
Julien Blin
ย 
PPTX
Prรฉsentation Git & GitHub
Thibault Vlacich
ย 
PPTX
Test-Driven Development In Action
Jon Kruger
ย 
PDF
Introduction Professional Scrum Developer for Java
Joris De Winne
ย 
PDF
Tutoriel GIT
Francois ANDRE
ย 
PPT
Tests Logiciel
Nathaniel Richand
ย 
PDF
Java Day-2
People Strategists
ย 
PDF
Java Day-7
People Strategists
ย 
PDF
Enum Report
enumplatform
ย 
PPTX
Data structures and algorithms lab5
Bianca TeลŸilฤƒ
ย 
PPSX
data structure(tree operations)
Waheed Khalid
ย 
Java 102 intro to object-oriented programming in java - exercises
agorolabs
ย 
Introduction to Agile
agorolabs
ย 
Computer Programming Overview
agorolabs
ย 
Java 103 intro to java data structures
agorolabs
ย 
C'est si simple git ! (devoxx france 2012)
Ulrich VACHON
ย 
Test driven-development
David Paluy
ย 
Mix it 2016 - Software craftsmanship : le chainon manquant de lโ€™agilitรฉ ?
Nicolas Ruffel
ย 
TDD - Short for Test Driven Development!
Bradley Cypert
ย 
Prรฉsentation du versioning avec Git
msadouni
ย 
Prรฉsentation de git
Julien Blin
ย 
Prรฉsentation Git & GitHub
Thibault Vlacich
ย 
Test-Driven Development In Action
Jon Kruger
ย 
Introduction Professional Scrum Developer for Java
Joris De Winne
ย 
Tutoriel GIT
Francois ANDRE
ย 
Tests Logiciel
Nathaniel Richand
ย 
Java Day-2
People Strategists
ย 
Java Day-7
People Strategists
ย 
Enum Report
enumplatform
ย 
Data structures and algorithms lab5
Bianca TeลŸilฤƒ
ย 
data structure(tree operations)
Waheed Khalid
ย 
Ad

Similar to Java 201 Intro to Test Driven Development in Java (20)

PPTX
The Test way
Mikhail Grinfeld
ย 
PDF
J.unit.action.2
chandra mouli
ย 
ODP
xUnit and TDD: Why and How in Enterprise Software, August 2012
Justin Gordon
ย 
PDF
Test Essentials @mdevcon 2012
Maxim Zaks
ย 
PPTX
TDD - Agile
harinderpisces
ย 
PPSX
Junit
FAROOK Samath
ย 
PDF
junit-160729073220 eclipse software testing.pdf
KomalSinghGill
ย 
PPTX
Test Driven Development - a Practitionerโ€™s Perspective
Malinda Kapuruge
ย 
PDF
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
ย 
PDF
An Introduction to Test Driven Development
CodeOps Technologies LLP
ย 
ODP
Testing In Java4278
contact.bsingh
ย 
ODP
Testing In Java
David Noble
ย 
PPTX
Test Driven Development
Sergey Aganezov
ย 
KEY
Unit test
Shuji Watanabe
ย 
PPT
Introduction to Behavior Driven Development
Robin O'Brien
ย 
PPTX
8-testing.pptx
ssuserd0fdaa
ย 
PDF
JUnit Testing Framework A Complete Guide.pdf
flufftailshop
ย 
PDF
JUnit Testing Framework A Complete Guide.pdf
flufftailshop
ย 
PDF
Java Beginners Meetup February 2017: Testing and TDD
Patrick Kostjens
ย 
PPTX
Test-Driven Development
John Blum
ย 
The Test way
Mikhail Grinfeld
ย 
J.unit.action.2
chandra mouli
ย 
xUnit and TDD: Why and How in Enterprise Software, August 2012
Justin Gordon
ย 
Test Essentials @mdevcon 2012
Maxim Zaks
ย 
TDD - Agile
harinderpisces
ย 
Junit
FAROOK Samath
ย 
junit-160729073220 eclipse software testing.pdf
KomalSinghGill
ย 
Test Driven Development - a Practitionerโ€™s Perspective
Malinda Kapuruge
ย 
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
ย 
An Introduction to Test Driven Development
CodeOps Technologies LLP
ย 
Testing In Java4278
contact.bsingh
ย 
Testing In Java
David Noble
ย 
Test Driven Development
Sergey Aganezov
ย 
Unit test
Shuji Watanabe
ย 
Introduction to Behavior Driven Development
Robin O'Brien
ย 
8-testing.pptx
ssuserd0fdaa
ย 
JUnit Testing Framework A Complete Guide.pdf
flufftailshop
ย 
JUnit Testing Framework A Complete Guide.pdf
flufftailshop
ย 
Java Beginners Meetup February 2017: Testing and TDD
Patrick Kostjens
ย 
Test-Driven Development
John Blum
ย 
Ad

Recently uploaded (20)

PDF
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
ย 
PDF
Code Once; Run Everywhere - A Beginnerโ€™s Journey with React Native
Hasitha Walpola
ย 
PPTX
Introduction to web development | MERN Stack
JosephLiyon
ย 
PPTX
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
ย 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
ย 
PDF
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
PDF
Which Hiring Management Tools Offer the Best ROI?
HireME
ย 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
ย 
PPTX
Agentforce โ€“ TDX 2025 Hackathon Achievement
GetOnCRM Solutions
ย 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
PPTX
arctitecture application system design os dsa
za241967
ย 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
PDF
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
PPTX
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 
PDF
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
ย 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
ย 
PPTX
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 
The Next-Gen HMIS Software AI, Blockchain & Cloud for Housing.pdf
Prudence B2B
ย 
Code Once; Run Everywhere - A Beginnerโ€™s Journey with React Native
Hasitha Walpola
ย 
Introduction to web development | MERN Stack
JosephLiyon
ย 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
ย 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
ย 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
ย 
Automated Test Case Repair Using Language Models
Lionel Briand
ย 
Which Hiring Management Tools Offer the Best ROI?
HireME
ย 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
ย 
Agentforce โ€“ TDX 2025 Hackathon Achievement
GetOnCRM Solutions
ย 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
ย 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
ย 
arctitecture application system design os dsa
za241967
ย 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
ย 
Alur Perkembangan Software dan Jaringan Komputer
ssuser754303
ย 
CV-Project_2024 version 01222222222.pptx
MohammadSiddiqui70
ย 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
ย 
IObit Uninstaller Pro 14.3.1.8 Crack for Windows Latest
utfefguu
ย 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
ย 
IObit Uninstaller Pro 14.3.1.8 Crack Free Download 2025
sdfger qwerty
ย 

Java 201 Intro to Test Driven Development in Java

  • 1. Java 201 โ€“ Intro to Test-Driven Development in Java
  • 2. Agenda โ€ข TDD Overview โ€ข Test Frameworks โ€ข Setting Up your IDE โ€ข Writing your first Unit Test โ€ข Anatomy of a Unit Test โ€ข Hands-on Exercise
  • 3. Intro to Test-Driven Development in Java TDD Overview
  • 4. TDD Overview โ€ข What is TDD โ€ข Why TDD โ€ข Feedback Loop โ€ข TDD Workflow
  • 5. What is TDD? โ€ข Technique for specifying system features with tests โ€ข Tests are written before production code โ€ข Simple, repeated, short-cycled mechanism โ€ข Automated mechanism for regression testing of code changes
  • 6. Why TDD? โ€ข Improves code quality โ€ข Minimises defects โ€ข Increases code maintainability โ€ข Reduces the cost of change โ€ข Provides a quick feedback โ€ข Acts as documentation for your code โ€ข Reduces fear of breaking things โ€ข It is fun and infectious ๏Š
  • 8. PDCA Explained โ€ข PLAN โ€“ Establish the objectives โ€“ Identify steps required to meet the objective โ€ข DO โ€“ Execute the steps to meet the objective โ€ข CHECK โ€“ Study actual results and compare against expected results โ€“ Look for completeness in meeting the objectives โ€ข ACT โ€“ If CHECK step shows objectives met vs. with the plan, repeat with next objective else rework and CHECK again
  • 10. Intro to Test-Driven Development in Java Test Frameworks
  • 11. What is a Test Framework? โ€ข An abstract set ofโ€ฆ โ€“ concepts, processes, procedures, environments โ€ข Where automated tests areโ€ฆ โ€“ designed, implemented and executed โ€ข Includes physical structures forโ€ฆ โ€“ test creation โ€“ test execution โ€ข As well asโ€ฆ โ€“ logical interactions amongst components
  • 12. Why Test Frameworks? โ€ข Provides the basis of test automation โ€ข Simplifies test automation efforts โ€ข Provides concepts and tools that support automated testing
  • 13. Common Test Frameworks โ€ข Code Testing โ€“ Junit (http://www.junit.org) โ€“ TestNG (http://testng.org) โ€ข UI Testing โ€“ Selenium (www.selenium.org) โ€“ Appium (http://appium.io/) โ€ข API Testing โ€“ SoapUI (http://www.soapui.org) โ€“ RestAssured (https://code.google.com/p/rest-assured)
  • 14. Intro to Test-Driven Development in Java Setting Up your IDE
  • 15. Setting Up โ€ข Required Software โ€ข Download JUnit โ€ข Create Java201 Project โ€ข Add JUnit to the Classpath โ€ข Create HelloGreetingTest โ€ข Run the HelloGreetingTest โ€ข Create the HelloGreeting class โ€ข Re-run the HelloGreetingTest
  • 16. Required Software โ€ข IDE โ€ข Java SDK โ€“ http://www.oracle.com/technetwork/java/javase/ downloads/jdk8-downloads-2133151.html โ€ข JUnit โ€“ www.junit.org
  • 17. Download JUnit โ€ข Download the following files from www.junit.org to a folder on your hard drive โ€“ junit.jar โ€“ hamcrest-core.jar
  • 18. Create Java201 Project โ€ข Create new project named Java201 โ€ข Create a new source folder named test in the java201 project โ€ข Create a lib folder in the Java201 project โ€ข In the test source folder, create the java201.greetings package
  • 19. Add JUnit to the Classpath โ€ข Copy the junit.jar and hamcrest-core.jar files to the lib folder of your project โ€ข Refresh the project to import the new contents of the lib folder โ€ข In Eclipse, click on the Java201 project, select File>Properties>Build Path and click on Libraries tab โ€ข Click Add JARs, browse to the project lib folder, select both jar files and click OK twice
  • 20. Create HelloGreetingTest.java โ€ข In Eclipse, Right-click the test folder, select New>JUnit Test Case, enter HelloGreetingTest in the Name field and click Finish
  • 23. Run the HelloGreetingTest โ€ข In Eclipse, Right-click the HelloGreetingTest and select Run As>JUnit Test
  • 24. Create HelloGreeting class โ€ข In Eclipse, right-click the src folder and select New>Class, enter HelloGreeting as class name and click Finish โ€ข Create a sayHello() method that returns the String โ€œHello, TDD!โ€
  • 25. Re-run the HelloGreetingTest โ€ข Declare and initialise HelloGreeting object and assert it returns โ€œHello, TDD!โ€ and rerun the test
  • 27. Intro to Test-Driven Development in Java Anatomy of a Unit Test
  • 28. Anatomy of a Unit Test Declaration of class under test setUp() runs once before each test method A test method Initialization of class under test tearDown() runs once after each test method Checking test results
  • 29. Intro to Test-Driven Development in Java Hands-on Exercise
  • 30. Exercise: Game of Cards โ€ข Using TDD, create the following building blocks of a typical card game โ€“ Card โ€ข Has a suite (Club, Heart, Diamond, Spade) and rank (2-10, Jack, Queen, King, Ace) โ€ข Ace is highest card โ€“ Hand โ€ข Consists of one or more Cards โ€“ Deck โ€ข Contains 52 Cards, 13 cards per suite โ€“ Dealer โ€ข Deals cards from a deck โ€ข Each player will be dealt a Hand of Cards
  • 31. Card rank suite getRank() : Rank setRank() : void getSuite() : Suite setSuite() : void Hand cards maxCards getNoOfCards() : int drawCard(): Card addCard() : void Deck cards getCards(int noOFCards) : List<Card> Dealer deck deal(noOfCards) : Hand Game of Cards Domain Model
  • 32. Solution: Game of Cards https://github.com/hawkmanacademy/java201
  • 33. Resources โ€ข UML Distilled - http://amzn.to/1BBHcXi โ€ข Test Driven Development โ€“ http://amzn.to/1xqPllN โ€ข XUnit Patterns - http://xunitpatterns.com/ โ€ข Junit โ€“ www.junit.org

Editor's Notes

  • #7: Tests Keep you out of the (time hungry) debugger! Tests Reduce Bugs in New Features Tests Reduce Bugs in Existing Features Tests Reduce the Cost of Change Tests Improve Design Tests Allow Refactoring Tests Constrain Features Tests Defend Against Other Programmers Testing Is Fun Testing Forces You to Slow Down and Think Testing Makes Development Faster Tests Reduce Fear