java 201 intro to test driven development in java
TRANSCRIPT
![Page 1: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/1.jpg)
Java 201 – Intro to Test-Driven Development in Java
![Page 2: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/2.jpg)
Agenda
• TDD Overview• Test Frameworks• Setting Up your IDE• Writing your first Unit Test• Anatomy of a Unit Test• Hands-on Exercise
![Page 3: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/3.jpg)
Intro to Test-Driven Development in Java
TDD Overview
![Page 4: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/4.jpg)
TDD Overview
• What is TDD• Why TDD• Feedback Loop• TDD Workflow
![Page 5: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/5.jpg)
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
![Page 6: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/6.jpg)
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
![Page 7: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/7.jpg)
Feedback Loop
![Page 8: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/8.jpg)
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
![Page 9: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/9.jpg)
TDD Workflow
![Page 10: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/10.jpg)
Intro to Test-Driven Development in Java
Test Frameworks
![Page 11: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/11.jpg)
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
![Page 12: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/12.jpg)
Why Test Frameworks?
• Provides the basis of test automation• Simplifies test automation efforts• Provides concepts and tools that support
automated testing
![Page 13: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/13.jpg)
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)
![Page 14: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/14.jpg)
Intro to Test-Driven Development in Java
Setting Up your IDE
![Page 15: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/15.jpg)
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
![Page 16: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/16.jpg)
Required Software
• IDE • Java SDK– http://www.oracle.com/technetwork/java/javase/
downloads/jdk8-downloads-2133151.html
• JUnit– www.junit.org
![Page 17: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/17.jpg)
Download JUnit
• Download the following files from www.junit.org to a folder on your hard drive – junit.jar– hamcrest-core.jar
![Page 18: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/18.jpg)
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
![Page 19: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/19.jpg)
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
![Page 20: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/20.jpg)
Create HelloGreetingTest.java• In Eclipse, Right-click the test folder, select New>JUnit Test Case,
enter HelloGreetingTest in the Name field and click Finish
![Page 21: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/21.jpg)
HelloGreetingTest.java
![Page 22: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/22.jpg)
Implement HelloGreetingTest.java
![Page 23: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/23.jpg)
Run the HelloGreetingTest• In Eclipse, Right-click the HelloGreetingTest and select Run
As>JUnit Test
![Page 24: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/24.jpg)
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!”
![Page 25: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/25.jpg)
Re-run the HelloGreetingTest
• Declare and initialise HelloGreeting object and assert it returns “Hello, TDD!” and rerun the test
![Page 26: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/26.jpg)
![Page 27: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/27.jpg)
Intro to Test-Driven Development in Java
Anatomy of a Unit Test
![Page 28: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/28.jpg)
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
![Page 29: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/29.jpg)
Intro to Test-Driven Development in Java
Hands-on Exercise
![Page 30: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/30.jpg)
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
![Page 31: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/31.jpg)
Card
ranksuitegetRank() : RanksetRank() : voidgetSuite() : SuitesetSuite() : void
Hand
cardsmaxCards
getNoOfCards() : intdrawCard(): CardaddCard() : void
Deck
cards
getCards(int noOFCards) : List<Card>
Dealer
deck
deal(noOfCards) : Hand
Game of Cards Domain Model
![Page 32: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/32.jpg)
Solution: Game of Cards
https://github.com/hawkmanacademy/java201
![Page 33: Java 201 Intro to Test Driven Development in Java](https://reader035.vdocuments.us/reader035/viewer/2022062220/55c3af2dbb61eb18678b4800/html5/thumbnails/33.jpg)
Resources
• UML Distilled - http://amzn.to/1BBHcXi • Test Driven Development – http://amzn.to/
1xqPllN • XUnit Patterns - http://xunitpatterns.com/• Junit – www.junit.org