Testing Driven Development. Advanced Programming

Similar documents
Java Foundations: Unit 3. Parts of a Java Program

White-box testing. Software Reliability and Testing - Barbara Russo SERG - Laboratory of Empirical Software Engineering.

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Ohad Barzilay and Oranit Dror

JAVA MOCK TEST JAVA MOCK TEST IV

15CS45 : OBJECT ORIENTED CONCEPTS

String. Other languages that implement strings as character arrays

Oracle 1Z Java Standard Edition 5 Programmer Certified Professional.

Selected Questions from by Nageshwara Rao

Efficient Java (with Stratosphere) Arvid Heise, Large Scale Duplicate Detection

Introduction to Java

Mutable and Immutable Objects

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

For more details on SUN Certifications, visit

White box testing. White-box testing. Types of WBT 24/03/15. Advanced Programming

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

PESIT Bangalore South Campus

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

20 Most Important Java Programming Interview Questions. Powered by

- Thus there is a String class (a large class)

Informatik II (D-ITET)

[Course Overview] After completing this module you are ready to: Develop Desktop applications, Networking & Multi-threaded programs in java.

infix expressions (review)

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

CSCI 201L Written Exam #1 Fall % of course grade

Grafting Functional Support on Top of an Imperative Language

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

COE318 Lecture Notes Week 4 (Sept 26, 2011)

2. All the strings gets collected in a special memory are for Strings called " String constant pool".

Programmazione di sistemi multicore

COMP 110/L Lecture 13. Kyle Dewey

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Java 8 Programming for OO Experienced Developers

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

TEST (MODULE:- 1 and 2)

STRINGS AND STRINGBUILDERS. Spring 2019

Fall 2017 CISC124 10/1/2017

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

Advanced Java Concepts Unit 2: Linked Lists.

BM214E Object Oriented Programming Lecture 8

WES-CS GROUP MEETING #9

Basic Principles of analysis and testing software

1993: renamed "Java"; use in a browser instead of a microwave : Sun sues Microsoft multiple times over Java

Testing & Continuous Integration. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 20 03/19/2010

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Threads Cannot Be Implemented As a Library

needs to be reliable, easy to change, retarget efficiency is secondary implemented as interpreter, with virtual machine

Programming II (CS300)

Advanced concurrent programming in Java Shared objects

Design Approaches for Concurrent Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 02/09/2012


Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Introduction to Programming Using Java (98-388)

OHJ-306x: Software Testing Introduction to the Course Project Part 1: General Information and Project phases 1 & 2: Unit testing

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

CMSC 132: Object-Oriented Programming II. Effective Java. Department of Computer Science University of Maryland, College Park

Welcome to the Using Objects lab!

Java reflection. alberto ferrari university of parma

ABOUT CORE JAVA COURSE SCOPE:

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

psd: A Scheme Debugger An Example

CSE332: Data Abstractions Lecture 25: Deadlocks and Additional Concurrency Issues. Tyler Robison Summer 2010

CmpSci 187: Programming with Data Structures Spring 2015

Informatik II (D-ITET)

Synchronization SPL/2010 SPL/20 1

Aim behind client server architecture Characteristics of client and server Types of architectures

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

From C++ to Java. Duke CPS

The Object Concept. Object-Oriented Programming Spring 2015

D Programming Language

Enough java.lang.string to Hang Ourselves...

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

JAVA SYLLABUS FOR 6 WEEKS

5/23/2015. Core Java Syllabus. VikRam ShaRma

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CSE 326: Data Structures. Section notes, 4/9/2009

PIC 20A The Basics of Java

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

ANALYZE OF PROGRAMMING TECHNIQUES FOR IMPROVEMENT OF JAVA CODE PERFORMANCE

The Dynamic Typing Interlude

CS 251 Intermediate Programming Java Basics

Lecture Notes on Memory Layout

CSE 332: Data Structures & Parallelism Lecture 18: Race Conditions & Deadlock. Ruth Anderson Autumn 2017

Object Reference and Memory Allocation. Questions:

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

What did we talk about last time? Course overview Policies Schedule

Java Programming Course Overview. Duration: 35 hours. Price: $900

Question 1. Notes on the Exam. Today. Comp 104: Operating Systems Concepts 11/05/2015. Revision Lectures

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

LOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee

ArrayList. Introduction. java.util.arraylist

CS 103 Lab V for Vector

Sharing Objects Ch. 3

Chapter 17 vector and Free Store. Bjarne Stroustrup

public Candy() { ingredients = new ArrayList<String>(); ingredients.add("sugar");

COMP 110/L Lecture 10. Kyle Dewey

Transcription:

Testing Driven Development Advanced Programming

Test Driven Development (TDD) Practice for writing unit tests and production code concurrently and at a very fine level of granularity Programmers first write a small portion of a unit test, and then they write just enough production code to make that unit test compile and execute 06/04/15 Barbara Russo 2

Test Driven Development (TDD) This cycle lasts somewhere between 30 seconds and five minutes. Rarely does it grow to ten minutes. In each cycle, the tests come first. Once a unit test is done, the developer goes on to the next test until they run out of tests for the task they are currently working on 06/04/15 Barbara Russo 3

Example - TDD TextFormatter: A text formatter that take arbitrary strings and horizontally center them in a page Few issues: What are the methods: setlinewidth() centerline() What is a Line? Can I use String? 06/04/15 Barbara Russo 4

What to test First understand the entities to test

String and StringBuffer String is immutable; that is, it cannot be modified once created If a String object is modified, a new String was actually created and the old one was thrown away. 06/04/15 6

Example String badlycuttext = " Java is great. "; System.out.println(badlyCutText); badlycuttext.trim(); //attempt to modify the string System.out.println(badlyCutText); Output Java is great. Java is great. 06/04/15 7

String The String.trim() method returns the string with leading and trailing whitespace removed The trim() method call does not modify the original object It creates a new trimmed String object and then throws it away Thus, we we print the string we get the original String object 06/04/15 8

String Once a String object is created, it can not be modified, takes up memory until garbage collection

To trim the original String String badlycuttext = " Java is great. "; System.out.println(badlyCutText); badlycuttext =badlycuttext.trim(); System.out.println(badlyCutText); Output Java is great. Java is great. 06/04/15 10

Using StringBuilder/StringBuffer With immutable objects, we need to store the modified object in a new reference variable 06/04/15 Barbara Russo 11

Raw concatenation public String converttostring(collection<string> words) { String str = ""; // Loops through every element in words collection for (String word : words) { str = str + word + " "; return str; 06/04/15 Barbara Russo 12

Raw concatenation On the + operation a new String object is created at each iteration. Suppose words contains the elements [ Foo, Bar, Bam, Baz ]. The method creates eleven Strings:, Foo,, Foo, Foo Bar,, Foo Bar, Foo Bar Bam,, Foo Bar Bam, Foo Bar Bam Baz Even though only the last one is actually useful. Memory is only cleaned by the garbage collector 06/04/15 Barbara Russo 13

Raw concatenation To avoid unnecessary memory use like this, use the StringBuilder class Only one StringBuilder object is created. Also because object creation is time consuming, using StringBuilder produces much faster code It provides similar functionality to Strings, but stores its data in a mutable way 06/04/15 Barbara Russo 14

Concatenation with StringBuilder public String converttostring(collection<string> words) { StringBuilder buffer = new StringBuilder(); // Loops through every element in words collection for (String word : words) { buffer.append(word); buffer.append(" "); return buffer.tostring(); 06/04/15 Barbara Russo 15

StringBuilder / StringBuffer As StringBuilder is not thread safe you cannot use it in more than one thread. Use StringBuffer instead, which does the same and is thread safe StringBuffers are thread-safe: they have synchronized methods to control access so that only one thread can access a StringBuffer object's synchronized code at a time. 06/04/15 Barbara Russo 16

StringBuffer However, as StringBuffer is slower, only use StringBuffer in a multi-thread environment Note: only StringBuffer exists before Java 5 06/04/15 Barbara Russo 17

StringBuilder If you are working in a single-threaded environment, using StringBuilder instead of StringBuffer may result in increased performance. So, prefer StringBuilder because, Small performance gain. StringBuilder is a 1:1 drop-in replacement for the StringBuffer class. StringBuilder is not thread synchronized and therefore performs better on most implementations of Java 06/04/15 Barbara Russo 18

First we write the test public void testcenterline(){ Formatter f = new Formatter(); does not compile public void testcenterline(){ Formatter f = new Formatter(); f.setlinewidth(10); assertequals(" word ", f.center("word")); does not compile class Formatter{ Then we write the production code compiles and passes class Formatter{ public void setlinewidth(int width) { public String center(string line) { return ""; compiles and fails public void testcenterline() { Formatter f = new Formatter(); f.setlinewidth(10); assertequals(" word ", f.center("word")); public void testoddcenterline() { Formatter f = new Formatter(); f.setlinewidth(10); assertequals( " hello ", f.center("hello")); import java.util.arrays; public class Formatter { private int width; private char spaces[]; public void setlinewidth(int width) { this.width = width; spaces = new char[width]; Arrays.fill(spaces, ' '); public String center(string term) { StringBuffer b = new StringBuffer(); int padding = width/2 - term.length(); b.append(spaces, 0, padding); b.append(term); b.append(spaces, 0, padding); return b.tostring(); compiles and unexpectedly fails public String center(string term) { StringBuffer b = new StringBuffer(); int padding = (width - term.length()) / 2; b.append(spaces, 0, padding); b.append(term); b.append(spaces, 0, padding); return b.tostring(); compiles and passes 06/04/15 compiles and passes Barbara Russo 19 compiles and fails public String center(string term) { int remainder = 0; StringBuffer b = new StringBuffer(); int padding = (width - term.length()) / 2; remainder = term.length() % 2; b.append(spaces, 0, padding); b.append(term); b.append(spaces, 0, padding + remainder); return b.tostring();

Exercise Extend the previous example by allowing any line length 06/04/15 Barbara Russo 20

Exercise Extend the example above by allowing terms that are concatenation of word 06/04/15 Barbara Russo 21

What are the benefits of TDD? Line Test Coverage: If you follow the rules of TDD, then virtually 100% of the lines of code in your production program will be covered by unit tests This does not cover 100% of the paths through the code, but it does make sure that virtually every line is executed and tested. 06/04/15 Barbara Russo 22

What are the benefits of TDD? Test Repeatability. The tests can be run any time you like. Documentation. The tests describe your understanding of how the code should behave. They also describe the API. Therefore, the tests are a form of documentation. 06/04/15 Barbara Russo 23

What are the benefits of TDD? API Design. When you write tests first, you put yourself in the position of a user of your program's API. This can only help you design that API better. Reduced Debugging. When you move in the tiny little steps recommended by TDD, it is hardly ever necessary to use the debugger. Debugging time is reduced enormously. 06/04/15 Barbara Russo 24