Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

A practical guide John E. Boal TestDrivenDeveloper.com.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
You want me to do what??? Refactoring legacy applications aka : fixing someone else’s “bad” code Niel Zeeman Team Foundation Consulting
BTS530: Major Project Planning and Design Iterative Development References: Agile & Iterative Development, by Craig Larman, 2004, Addison Wesley. Agile.
COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Chapter 21 Test Driven development. TDD Write the test first Show that test fails Write code to pass the test Run whole suite of tests! This is unit testing.
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
25-Jun-15 Refactoring III. General philosophy A refactoring is just a way of rearranging code Refactorings are used to solve problems If there’s no problem,
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
Multiple Choice Solutions True/False a c b e d   T F.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Refactoring Cristescu Marilena. Definitions Loose Usage: Reorganize a program(or something) As a noun: a change made to the internal structure of some.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
Sadegh Aliakbary Sharif University of Technology Spring 2012.
CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the.
Chapter 18 Object Design Examples with GRASP 1CS6359 Fall 2011 John Cole.
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Refactoring Improving the structure of existing code Refactoring1.
17-Oct-15 Refactoring. 2 Refactoring is: restructuring (rearranging) code......in a series of small, semantics-preserving transformations (i.e. the code.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Refactoring1 Improving the structure of existing code.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Advanced Programming in Java
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Refactoring Advanced Software Engineering Dr Nuha El-Khalili.
Refactoring1 Improving the structure of existing code.
Refactoring. 2 Process of changing a software system in such a way that it does not alter the external behavior of the code, yet improves its internal.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Mapping Designs to Code. It specify how to map the design into object oriented language The UML artifacts created during the design work, the interaction.
Principles and examples
More Sophisticated Behavior
Module Road Map Refactoring Why Refactoring? Examples
Refactoring II 21-Sep-18.
Overview of Eclipse Lectures
Test Driven development
Test Driven development
Refactoring III 27-Nov-18.
Advanced Java Programming
Improving the structure of existing code
Chapter 4 Writing Classes.
Refactoring and Code Smells
Refactoring III 25-Dec-18.
Advanced Programming Behnam Hatami Fall 2017.
Java Inheritance.
Refactoring II 5-Feb-19.
CS 350 – Software Design Singleton – Chapter 21
Refactoring.
Presentation transcript:

Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Write the Tests First Tests are written before the code Developer writes unit tests for nearly all production code CS6359 Fall 2011 John Cole2

Advantages The tests actually get written Programmer satisfaction leads to more consistent test writing Clarification of detailed interface and behavior Provable, repeatable, automated verification Confidence to change things CS6359 Fall 2011 John Cole3

Example To test the Sale class, create a SaleTest class that: – Creates a Sale (the thing to be tested; a fixture) – Adds some line items to it with the makeLineItem method – Asks for the total and verifies that it is the expected value CS6359 Fall 2011 John Cole4

Pattern of Testing Methods Create the fixture Do some operation you want to test Evaluate the results Point: Don’t write all of the tests for Sale first; write one test method, implement the solution in Sale to make it pass, then repeat CS6359 Fall 2011 John Cole5

Using xUnit Create a class that extends the TestCase class Create a separate testing method for each Sale method you want to test. This will generally be every public method. CS6359 Fall 2011 John Cole6

Refactoring A structured, disciplined method to rewrite or restructure existing code without changing its external behavior, applying small transformations and re-testing each step. Unit tests applied after each step ensure that the changes didn’t cause a problem CS6359 Fall 2011 John Cole7

Goals of Refactoring Remove duplicate code Improve clarity Make long methods shorter Remove the use of hard-coded literal constants CS6359 Fall 2011 John Cole8

Code Smells Duplicated code Big methods Class with many instance variables Class with lots of code Strikingly similar subclasses Little or no use of interfaces High coupling CS6359 Fall 2011 John Cole9

Basic Refactorings Extract method – Convert a long method into shorter ones by moving part of it into a helper method Extract constant – Replace a literal with a constant variable Introduce explaining variable – put the results of an expression into a temporary variable with a name that explains its purpose Replace constructor call with Factory Method – CS6359 Fall 2011 John Cole10

More Refactorings Change method parameters (changes them in the definition, then finds all references) Replace using new with a call to a method that returns an object. (Factory pattern) CS6359 Fall 2011 John Cole11

Code example Public class Die { public static final int MAX=6; public int faceValue; public Die(){ roll(); } Public void roll() { faceValue = (int)((Math.Random() * MAX) + 1; } Public int getFaceValue() { return faceValue; } CS6359 Fall 2011 John Cole12

Another Example rollDice on 392 CS6359 Fall 2011 John Cole13