CSC 213 – Large Scale Programming. Why Do We Test?

Slides:



Advertisements
Similar presentations
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Advertisements

Method Parameters and Overloading. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
CSC1016 Coursework Clarification Derek Mortimer March 2010.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Professor John Peterson
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Test. A software house decides to develop a DVD renting program. The product manager identifies the following requirements: Every DVD will have a title,
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Problem Of The Day  Decipher the following phrase: STANDS 0 _
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
Exceptions, cont’d. Factory Design Pattern COMP 401 Fall 2014 Lecture 12 9/30/2014.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming –TextField Action Listeners, JEditorPane action listeners, HTML in a JEditorPane,
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
CSC 213 – Large Scale Programming. Today’s Goal  Understand why testing code is important  Result of poor or no testing & embarrassment caused  Learn.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
Question of the Day completes starts  What word completes the 1 st word & starts the 2 nd one: DON???CAR.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Software Design– Unit Testing SIMPLE PRIMER ON Junit Junit is a free simple library that is added to Eclipse to all automated unit tests. The first step,
CSC Programming for Science Lecture 18: More Data Files.
Bjarne Stroustrup I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.
Problem of the Day  Why are manhole covers round?
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Reference: Object Oriented Design and Programming (Horstmann)
Method Parameters and Overloading
Computer Science 209 Testing With JUnit.
Java Programming: Guided Learning with Early Objects
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Java Programming Language
slides created by Ethan Apter
1. Open Visual Studio 2008.
slides created by Ethan Apter
Designing For Testability
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
slides created by Ethan Apter and Marty Stepp
Presentation transcript:

CSC 213 – Large Scale Programming

Why Do We Test?

But the Problem Is…

Can We Get Best Of Both?

Testing To The Rescue  Before we start coding, write solid set of tests  Best, worst, & average cases examined in tests  Includes tests for all possible boundary conditions  Tests drive the code & set pace of the coding  Code only written to be good enough to pass tests  Debugging simplified using the simple failed cases  Once system can pass all tests, we’re golden

Testing To The Rescue  Before we start coding, write solid set of tests  Best, worst, & average cases examined in tests  Includes tests for all possible boundary conditions  Tests drive the code & set pace of the coding  Code only written to be good enough to pass tests  Debugging simplified using the simple failed cases  Once system can pass all tests, we’re golden

Testing To The Rescue  Before we start coding, write solid set of tests  Best, worst, & average cases examined in tests  Includes tests for all possible boundary conditions  Tests drive the code & set pace of the coding  Code only written to be good enough to pass tests  Debugging simplified using the simple failed cases  Once system can pass all tests, we’re golden

Oops…  Design we created has lots of side effects  Results printed out at the end of void methods  Other methods just update values of object’s fields  Add special methods that allow field access  Not a big deal & needed to test affect on fields' values

OOPS…  Stuck with BIG issue: how to check I/O correct?  How to test screen to verify warning are printed?  How to insure file has correct values before & after?

OOPS…  Stuck with BIG issue: how to check I/O correct?  How to test screen to verify warning are printed?  How to insure file has correct values before & after?  Cannot skip tests since correctness important  Always want to be correct, but cannot test online

OOPS…  Stuck with BIG issue: how to check I/O correct?  How to test screen to verify warning are printed?  How to insure file has correct values before & after?  Cannot skip tests since correctness important  Always want to be correct, but cannot test online And it monitors a nuclear reactor

Laziness Depends on Design  Well-planned design makes testable methods  Testable methods leads to good tests  Good tests leads to easy coding & debugging  Easy coding & debugging leads to free time  Free time leads to laziness

Design Methods For Testing  Start by defining method for each calculation  Method returns result once it is calculated  As needed, create other methods to output results  Easy to test & debug methods with observable results

Design Methods For Testing  Start by defining method for each calculation  Method returns result once it is calculated  As needed, create other methods to output results  Easy to test & debug methods with observable results versus

Mock Objects To Win  Some things just cannot be tested with JUnit Failed nuclear plant tests a bit of a problem  Some times its impossible to access or use classes  File or network I/O required, but cannot test offline  These situations leave limited options  Whine about it being unfair  Leave code untested and say "oops" a lot  Prove correctness mathematically  Create stand-ins called mock objects

Getting Ready for Mocks  Use interfaces in place of hard-to-use classes  Interface defines minimum number of methods  Update old class to implement this interface  Must modify code when hard-to-use class alloc'd  Pass instance to method & eliminate new command  If instance was field, set using constructor's parameter

Original Example public class ThreeMileIsland { private NuclearReactor reactor; public ThreeMileIsland() { reactor = new NuclearReactor("SNPP"); } public boolean checkForBreach() { if (!reactor.withinLimits()) { return reactor.alarmSounding(); } return false; } }

New Interface public interface NRInterface { public boolean withinLimits(); public boolean alarmSounding(); } public class ThreeMileIsland { // More code went here public boolean checkForBreach() { if (!reactor.withinLimits()) { return reactor.alarmSounding(); } return false; } }

Rewrite From This… public class ThreeMileIsland { private NuclearReactor reactor; public ThreeMileIsland() { reactor = new NuclearReactor("SNPP"); } public boolean checkForBreach() { if (!reactor.withinLimits()) { return reactor.alarmSounding(); } return false; } }

Rewrite From This… …To This public class ThreeMileIsland { private NRInterface reactor; public ThreeMileIsland(NRInterface nm) { reactor = nm; } public boolean checkForBreach() { if (!reactor.withinLimits()) { return reactor.alarmSounding(); } return false; } }

Why We Write Mock Classes!

Create Mock Class  Write classes needed to test code fully  Testing ThreeMileIsland not NuclearReactor  ONLY purpose is testing  ONLY purpose is testing other classes code  May hard-code values; write as simply as possible  Store parameters in Sequence s to check calls correct  Do not use files or network, mock to avoid these needs  Duct tape & mock classes are similar  Useful & pretty, but not the real thing

Writing Mock Class public interface NRInterface { public boolean withinLimits(); public boolean alarmSounding(); } public class MeltdownMock implements NRInterface{ public boolean withinLimits() { return false; } public boolean alarmSounding() { return true; } }

Using Mock Class

Writing Mock Class public class MeltdownMock implements NRInterface { public boolean withinLimits() { return false; } public boolean alarmSounding() { return true; } public void testTMI() { NRInterface mltdn = new MeltdownMock(); ThreeMileIsland tmi = new ThreeMileIsland(mltdn); assertTrue(tmi.checkForBreach()); }

Mock Class Coding Review  Use interfaces in place of hard-to-use classes  Interface defines minimum number of methods  Update old class to implement this interface  Must modify code when hard-to-use class alloc'd  Pass instance to method & eliminate new command  If instance was field, set using constructor's parameter  Test original using specially written mock classes  Mock classes should be simple, often return a constant  Not testing mocking, but if class can handle its result

Testing Printing To Screen  Java already thought of this & created solution  Only need to set static field in System class  System.setOut(PrintStream) does this for us  Instance passed as parameter will be sent all output  No new classes required for this to work  Can add code to method run before each test method needed to contain this code

Writing Code to Test Output

For Next Lecture  Next weekly assignment available online  Due as usual tomorrow at 5PM  Give me notes on reading to delay this due date  Reading on event-driven programming  How to code if we do not know what will happen?  What error is found most starting GUI programs?  How can I make my code finish within my lifetime?  (Design notes for ActionListener okay for extension)