Java 201 Intro to Test Driven Development in Java

33
Java 201 – Intro to Test-Driven Development in Java

Transcript of Java 201 Intro to Test Driven Development in Java

Page 1: Java 201   Intro to Test Driven Development in Java

Java 201 – Intro to Test-Driven Development in Java

Page 2: 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

Page 3: Java 201   Intro to Test Driven Development in Java

Intro to Test-Driven Development in Java

TDD Overview

Page 4: Java 201   Intro to Test Driven Development in Java

TDD Overview

• What is TDD• Why TDD• Feedback Loop• TDD Workflow

Page 5: Java 201   Intro to Test Driven Development in Java

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

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

Feedback Loop

Page 8: Java 201   Intro to Test Driven Development in Java

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

TDD Workflow

Page 10: Java 201   Intro to Test Driven Development in Java

Intro to Test-Driven Development in Java

Test Frameworks

Page 11: Java 201   Intro to Test Driven Development in Java

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

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

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

Intro to Test-Driven Development in Java

Setting Up your IDE

Page 15: Java 201   Intro to Test Driven Development in Java

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 17: Java 201   Intro to Test Driven Development in Java

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

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

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

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

HelloGreetingTest.java

Page 22: Java 201   Intro to Test Driven Development in Java

Implement HelloGreetingTest.java

Page 23: Java 201   Intro to Test Driven Development in Java

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

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

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
Page 27: Java 201   Intro to Test Driven Development in Java

Intro to Test-Driven Development in Java

Anatomy of a Unit Test

Page 28: Java 201   Intro to Test Driven Development in Java

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

Intro to Test-Driven Development in Java

Hands-on Exercise

Page 30: Java 201   Intro to Test Driven Development in Java

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

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

Solution: Game of Cards

https://github.com/hawkmanacademy/java201

Page 33: Java 201   Intro to Test Driven Development in Java

Resources

• UML Distilled - http://amzn.to/1BBHcXi • Test Driven Development – http://amzn.to/

1xqPllN • XUnit Patterns - http://xunitpatterns.com/• Junit – www.junit.org