Chapter 2 Additional Features of Java Programming.

Slides:



Advertisements
Similar presentations
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
J-Unit Framework.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Important Notes Pay attention to file names!!! You must name them what we tell you to. mkdir – make a folder (make the ones we tell you to) rm – remove.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
14-Jul-15 JUnit 4. Comparing JUnit 3 to JUnit 4 All the old assertXXX methods are the same Most things are about equally easy JUnit 4 makes it easier.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
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.
Computer Programming Lab(5).
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
CSE219, Computer Science III Stony Brook University Test-Driven Development (a.k.a. Design to Test) 1.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
CSE 219 Computer Science III Testing. Testing vs. Debugging Testing: Create and use scenarios which reveal incorrect behaviors –Design of test cases:
Programming Concept Chapter I Introduction to Java Programming.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
1 Intro to Java Week 12 (Slides courtesy of Charatan & Kans, chapter 8)
BUILDING JAVA PROGRAMS CHAPTER 1 ERRORS. 22 OBJECTIVES Recognize different errors that Java uses and how to fix them.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Unit Testing CSIS 3701: Advanced Object Oriented Programming.
WEEK 2 Introduction to Java II CSE 252 Principles of Programming Languages LAB SECTION.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Unit Testing with JUnit CS 3331 JUnit website at Kent Beck and Eric Gamma. Test Infected: Programmers Love Writing Tests, Java Report,
Building Java Programs Chapter 15 Lecture 15-2: testing ArrayIntList; pre/post conditions and exceptions reading:
Error Handling Tonga Institute of Higher Education.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
SE 433/333 Software Testing & Quality Assurance Dennis Mumaugh, Instructor Office: CDM, Room 428 Office Hours: Tuesday, 4:00 –
Justin Bare and Deric Pang with material from Erin Peach, Nick Carney, Vinod Rathnam, Alex Mariakakis, Krysta Yousoufian, Mike Ernst, Kellen Donohue Section.
Inheritance ndex.html ndex.htmland “Java.
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
CSCB07 Fall Tutorial 6. Overriding Review Overriding works ONLY for methods, NOT variables! Car c2 = new Porsche(); Same as: Porsche p2 = new Porsche();
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Computer Science 209 Software Development Packages.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
Staples are our staple Building upon our solution.
Data Structures and Algorithms in JAVA Chapter 2.
CSE 143 Lecture 14: testing.
Building Java Programs
Object-Oriented Concepts
Software Construction Lab 10 Unit Testing with JUnit
Unit Testing with JUnit
Chapter 2 Clarifications
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Lecture 14 Throwing Custom Exceptions
Maha AlSaif Maryam AlQattan
Java Language Basics.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Credit to Eclipse Documentation
Introduction to JUnit CS 4501 / 6501 Software Testing
L3. Necessary Java Programming Techniques
L3. Necessary Java Programming Techniques
Sampath Kumar S Assistant Professor, SECE
Testing Acknowledgement: Original slides by Jory Denny.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Polymorphism Part 1
Building Java Programs
Presentation transcript:

Chapter 2 Additional Features of Java Programming

METHOD TESTING WITH JUnit

A method is correct if it satisfies its specification. Your confidence in the correctness of your methods is increased by testing, which can reveal the presence but not absence of errors.

JUnit is a free software package for testing a class’s methods.

For example, here is an indirect test of the 3-parameter constructor in the HourlyEmployee class:

@Test public void test3() { hourly = new HourlyEmployee ("terry", 41, 20.00); assertEquals (800.00, hourly.getRegularPay()); assertEquals (30.00, hourly.getOvertimePay()); assertEquals (830.00, hourly.getGrossPay()); } // method test3

import org.junit.*; import static org.junit.Assert.*; import org.junit.runner.Result; import static org.junit.runner.JUnitCore.runClasses; import java.util.*; public class HourlyEmployeeTest { public static void main(String[ ] args) { Result result = runClasses (HourlyEmployeeTest.class); System.out.println ("Tests run = " + result.getRunCount() + "\nTests failed = " + result.getFailures()); } // method main

@Test public void test1() { hourly = new HourlyEmployee ("andrew", 39, 10.00); assertEquals (390.00, hourly.getRegularPay()); assertEquals (0.00, hourly.getOvertimePay()); assertEquals (390.00, hourly.getGrossPay()); } // method public void test2() { hourly = new HourlyEmployee ("beth", 40, 20.00); assertEquals (800.00, hourly.getRegularPay()); assertEquals (0.00, hourly.getOvertimePay()); assertEquals (800.00, hourly.getGrossPay()); } // method test2

@Test public void test3() { hourly = new HourlyEmployee ("terry", 41, 20.00); assertEquals (800.00, hourly.getRegularPay()); assertEquals (30.00, hourly.getOvertimePay()); assertEquals (830.00, hourly.getGrossPay()); } // method public void test4() { hourly = new HourlyEmployee ("karen", 50, 10); assertEquals (400.00, hourly.getRegularPay()); assertEquals (150.00, hourly.getOvertimePay()); assertEquals (550.00, hourly.getGrossPay()); } // method test4 } // class HourlyEmployeeTest

TESTING PRINCIPLES Testing can reveal the presence of errors but not the absence of errors. A method’s tests should be developed before the method is defined. In general, methods should be designed to facilitate testing. Make sure that any boundary conditions are thoroughly tested. Good testing requires great skepticism.

Exception Handling

A robust program is one that does not terminate abnormally from invalid user input. An exception is an object created by an unusual condition, such as an attempt at invalid processing

Exercise: Define an equals method in the HourlyEmployee class to override the equals method in the Object class. Two HourlyEmployee objects are equal if they have the same name, hours worked and pay rate (but pay rates should not be compared for equality). Hint: If there is an HourlyEmployee object whose name is “Smith”, with 60 hours worked and a pay rate of $10.00 per hour, that employee should not be equal to another HourlyEmployee named “Smith” with 35 hours worked and a pay rate of $20.00 per hour. The gross pay is $ in both cases.