SlideShare ist ein Scribd-Unternehmen logo
1 von 33
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
Jussi Pohjolainen
 

Was ist angesagt? (12)

Core java Basics
Core java BasicsCore java Basics
Core java Basics
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Basic java for Android Developer
Basic java for Android DeveloperBasic java for Android Developer
Basic java for Android Developer
 
Scala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud FoundryScala, Play 2.0 & Cloud Foundry
Scala, Play 2.0 & Cloud Foundry
 
Core java
Core javaCore java
Core java
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 

Andere mochten auch

Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
Bianca Teşilă
 

Andere mochten auch (20)

Java 102 intro to object-oriented programming in java - exercises
Java 102   intro to object-oriented programming in java - exercisesJava 102   intro to object-oriented programming in java - exercises
Java 102 intro to object-oriented programming in java - exercises
 
Introduction to Agile
Introduction to AgileIntroduction to Agile
Introduction to Agile
 
Computer Programming Overview
Computer Programming OverviewComputer Programming Overview
Computer Programming Overview
 
Java 103 intro to java data structures
Java 103   intro to java data structuresJava 103   intro to java data structures
Java 103 intro to java data structures
 
C'est si simple git ! (devoxx france 2012)
C'est si simple git ! (devoxx france 2012)C'est si simple git ! (devoxx france 2012)
C'est si simple git ! (devoxx france 2012)
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
Mix it 2016 - Software craftsmanship : le chainon manquant de l’agilité ?
Mix it 2016 - Software craftsmanship : le chainon manquant de l’agilité ?Mix it 2016 - Software craftsmanship : le chainon manquant de l’agilité ?
Mix it 2016 - Software craftsmanship : le chainon manquant de l’agilité ?
 
TDD - Short for Test Driven Development!
TDD - Short for Test Driven Development!TDD - Short for Test Driven Development!
TDD - Short for Test Driven Development!
 
Présentation du versioning avec Git
Présentation du versioning avec GitPrésentation du versioning avec Git
Présentation du versioning avec Git
 
Présentation de git
Présentation de gitPrésentation de git
Présentation de git
 
Présentation Git & GitHub
Présentation Git & GitHubPrésentation Git & GitHub
Présentation Git & GitHub
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Introduction Professional Scrum Developer for Java
Introduction Professional Scrum Developer for JavaIntroduction Professional Scrum Developer for Java
Introduction Professional Scrum Developer for Java
 
Tutoriel GIT
Tutoriel GITTutoriel GIT
Tutoriel GIT
 
Tests Logiciel
Tests LogicielTests Logiciel
Tests Logiciel
 
Java Day-2
Java Day-2Java Day-2
Java Day-2
 
Java Day-7
Java Day-7Java Day-7
Java Day-7
 
Enum Report
Enum ReportEnum Report
Enum Report
 
Data structures and algorithms lab5
Data structures and algorithms lab5Data structures and algorithms lab5
Data structures and algorithms lab5
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 

Ähnlich wie Java 201 Intro to Test Driven Development in Java

Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Codecamp Romania
 

Ähnlich wie Java 201 Intro to Test Driven Development in Java (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Automated php unit testing in drupal 8
Automated php unit testing in drupal 8Automated php unit testing in drupal 8
Automated php unit testing in drupal 8
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!
 
Test Driven Development & CI/CD
Test Driven Development & CI/CDTest Driven Development & CI/CD
Test Driven Development & CI/CD
 
Getting Started with Selenium
Getting Started with SeleniumGetting Started with Selenium
Getting Started with Selenium
 
Unit Testing - Calgary .NET User Group - Nov 26 2014 - Depth Consulting
Unit Testing -  Calgary .NET User Group - Nov 26 2014 - Depth ConsultingUnit Testing -  Calgary .NET User Group - Nov 26 2014 - Depth Consulting
Unit Testing - Calgary .NET User Group - Nov 26 2014 - Depth Consulting
 
Agile Acceptance testing with Fitnesse
Agile Acceptance testing with FitnesseAgile Acceptance testing with Fitnesse
Agile Acceptance testing with Fitnesse
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
 
Battle for Code Quality - A Story of One Java Project
Battle for Code Quality - A Story of One Java ProjectBattle for Code Quality - A Story of One Java Project
Battle for Code Quality - A Story of One Java Project
 
How to use selenium successfully
How to use selenium successfullyHow to use selenium successfully
How to use selenium successfully
 
Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy Code
 
Episode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in SalesforceEpisode 5 - Writing unit tests in Salesforce
Episode 5 - Writing unit tests in Salesforce
 
Agile testing
Agile testingAgile testing
Agile testing
 
Continuous Testing
Continuous TestingContinuous Testing
Continuous Testing
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
Eclipse UI automation
Eclipse UI automationEclipse UI automation
Eclipse UI automation
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Selenium web driver_2.0_presentation
Selenium web driver_2.0_presentationSelenium web driver_2.0_presentation
Selenium web driver_2.0_presentation
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 

Kürzlich hochgeladen

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Kürzlich hochgeladen (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 

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
  • 26.
  • 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

Hinweis der Redaktion

  1. 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