Java 201 – Intro to Test-Driven Development in Java
Aug 07, 2015
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
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
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