Exercises : Testing Java with JUnit

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

User-Defined Functions Like short programs Can operate on their own data Can receive data from callers and return data to callers.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
Methods Java 5.1 A quick overview of methods
Pointers Pointer is a variable that contains the address of a variable Here P is sahd to point to the variable C C 7 34……
Chapter Five Functions
Modular Programming With Functions
Java Programming Abstract classes and Interfaces.
Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
Computer Programming Lab 8.
Ch. 2 Exploring core JUnit. This chapter covers ■ Using the core JUnit classes ■ Understanding JUnit mechanisms ■ Understanding the JUnit lifecycle.
Designing Classes Chapter 8. Classes Collection of objects Objects are not actions Class names – Nouns Method names – Verbs What Makes a Good Class Represent.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 23, 2011) Washington State University.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Computer Science 1620 Programming & Problem Solving.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
Adding and Subtracting Integers.
WHEN MULTIPLYING LIKE BASES, YOU ADD THE EXPONENTS FOR EXAMPLE: NOW YOU TRY:
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
ADDING INTEGERS Positive + Positive = Positive Positive + Positive = Positive ( +3) + (+2) = +5 ( +3) + (+2) = +5 When a number is positive, you do not.
Arithmetic Sequences A sequence in which each term after the first is obtained by adding a fixed number to the previous term is an arithmetic sequence.
Georgia Institute of Technology Introduction to Java part 2 Barb Ericson Georgia Institute of Technology May 2006.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
10/25: Methods & templates Return to concepts: methods Math class methods 1 st Program of the day About DrawLine.java modifications Method definitions.
Understand Application Lifecycle Management
Pre-Algebra 2-6 Exponents 2-6 Exponents Pre-Algebra Warm Up Warm Up Problem of the Day Problem of the Day Lesson Presentation Lesson Presentation.
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.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 Extending the Number System.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
2-6 Exponents Course 3 Warm Up Warm Up Problem of the Day Problem of the Day Lesson Presentation Lesson Presentation.
OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.
(2-2) Functions I H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (Spetember 9, 2015) Washington State University.
Example 2 Adding Integers Find the sum – CHECK You can use a number line to check your answer –= 8 – Use sign of number with greater absolute.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
Subtracting Integers. Warm Up 1.71 – 15 = (-15) = 3.53 – 45 = (-45) = 5.79 – 79 = (-79) = 7.Do you notice anything about these exercises?
Mathematical Calculations in Java Mrs. G. Chapman.
Calculations Chapter 11 Library of math functions, and constants cos, sin, tan, abs, min, max, log, random, sqrt, pow, exp Constants.PI,.E Use care with.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
Homework #2: Functions and Arrays By J. H. Wang Mar. 20, 2012.
10/25: Methods & templates Return to concepts: methods Math class methods Program of the day.
Mathematical Calculations in Java Mrs. C. Furman.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
The Math Class Methods Utilizing the Important Math Operations of Java!
3 + (-5) = (-5)=
Testing Chapter 23 IB103 Week 12 (part 3). Verify that a complex (any) program works correctly, that the program meets specifications The chapter reviews.
Homework #1: C++ Basics, Flow of Control, and Function Basics
Subtracting Integers 7 th Grade Math Pg DART statement: I can subtract integers.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Exercise 3 Example> Write a C function that implements /* input : integer value n output : */ int f ( int n ) { int i, sum=0; for (i=1;i
How to Write a Grid Service  The grid service will do basic mathematical operations. The math grid service is going to have the following methods:  add:
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
Classes - Intermediate
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,
Today you will need: A pencil 1 die Your math journal A partner.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
User-Written Functions
Chapter 7 User-Defined Methods.
A bit of C programming Lecture 3 Uli Raich.
Chapter 3 Assignment and Interactive Input.
Trainings 11/4 Intro to Java.
Java Lesson 36 Mr. Kalmes.
CSE 1020:Software Development
Incremental Programming
Presentation transcript:

Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

Exercises with JUnit: 1) Implement and test the “+” operator for pair of numbers 2) Extend the “Add” class for working with not a not fixed/limited set of numbers 3) Implement and test other mathematical operators (e.g., “-”, “*”, “/”, “sqrt”, “power”) 4) Test the CurrentAccount class 4b) Implement a change requirement into CurrentAccount 5) Write test suites … all the test cases must be written by using JUnit...

1) Implement and test the “+” operator for pairs of numbers - Create an eclipse project (called “MathOperators2”) Write a Java class called “Add” (in a packege called “math”) that implements the sum operator between pairs of integer numbers - The sum operators must take 2 integer numbers and return an integer In the eclipse project, create a package called “jUnitTest_X” Where “_X” could be equal to “3x” or “4x” according to the version of Junit in which the test cases will be used Add the Junit_X library to the current eclipse project Write unit test cases for testing the Java class - Try the class by using different inputs such us integer, double, and long Run the test cases, fix bugs (if any) and re-run until no bugs are revealed … constraints:

1) Implement and test the “+” operator for pairs of numbers (2) Please, during implementation and testing consider the following additional constraints: The output of the sum operator in the class “Add” must be of the same type of its inputs. E.g., when it receives two integer the output will be an integer. - Thus, it could be fine to implement different methods in the class that get pairs of different types of numbers, e.g., pairs of integer/double numbers,etc. When writing test cases try to use just one test method per input type (integer, double, long) with different pairs of number as input (use @RunWith and @Parameters)

… after the exercises 1 and 2 … 2) Extend the “Sum” class for working with a not limited/fixed set of numbers … after the exercises 1 and 2 … - Write a new class called “AddExtended” that takes a set of numbers (use a Vector<Double> as input) and returns the sum of them - Write test cases for exerciting the “+” operators by using limited and not limited sets of numbers -- e.g., sum three/five integer/double numbers instead of just two -- when writing test cases try to use just one test method per input type (integer, double, long) with different sets of numbers as input (use @RunWith and @Parameters) - Run the test cases, if there are bugs fix them and re-run until no bugs are in the class

3) Implement and test other mathematical operators (e. g. ,“-”, “ 3) Implement and test other mathematical operators (e.g.,“-”, “*”, “/”, “sqrt”, “power”) … after the exercises 1,2 and 3 … In mat, write a new Java class called “Subtract” that implements the subtraction between numbers Write unit test cases for the “Subtract” class - … and similarly for all the other operators (“-”, “*”, “/”, “sqrt”, “power”)

4) Test the CurrentAccount class - Given the following CurrentAccount class: class CurrentAccount { int account[]; int lastMove; CurrentAccount() { lastMove = 0; account = new int[10]; } public void deposit(int value) { account[lastMove] = value; lastMove++; public void draw(int value) { …… …. public int settlement() { int result = 0; for (int i=0; i<account.length; i++) { result = result + account[i]; return result; public static void main(String args[]) { CurrentAccount c = new CurrentAccount(); c.deposit(10);

4) Test the CurrentAccount class Create a new eclipse project called “CurrentAccount” Create the class “CurrentAccount” in a package “bankAccount” Write unit test cases for such class - (see next slide for constraints) Run the test cases, fix bugs (if any) and re-run until no bugs are Revealed Implement the following change requirements: “changing the data structure used in the class: Array --> Vector” - re-Run the test cases, fix bugs (if any) and re-run until no bugs are revealed

4) Test the CurrentAccount class - verify that your test suite include at least the following cases: * test cases for testing just the deposit operation it must be possible to deposit positive amount of money it must be forbidden to deposit negative amount of money * test cases for testing just the draw operation  it must be possible to draw negative amount of money  e.g., it must be forbidden to draw positive amount of money * test cases for testing just the settlement operation it depends on the other operations call settlment after short (<10) and long (>30) sequences of other operations before calling the settlment operation * test cases for testing sequences of the previous operations - Constraint: -- when writing test cases to test method and method combinations, exercise each method/combination with different sets of inputs (use @RunWith and @Parameters)

4b) Implement a change requirement into CurrentAccount - Implement and test the following change requirement: * Add a class called “CollectionOfAccounts” it contains a list of accounts (“LinkedList<CurrentAccount>”) it has a method to add account to this list it has a method to get the account number X in the list - Notice that X in an integer read from the user (0<X<list.size) it has a method to get the accounts in the list from 0 to X. In other terms the method returns the first X accounts that are in the list Notice that X in an integer read from the user (0<X<list.size) The set of X accounts must be returned into a collection of type: “Collection<CurrentAccount>” * Write test cases for the new class Initialize the list with 3 account class (with respectively 10, 20, 30 as initial deposit) Test each method of the class Constraints when writing test cases: - use @Before to initialize the accounts - use @RunWith and @Parameters to test method and method combinations with different sets of inputs

5) Write test suites For all test cases realted to mathematical operators and for the CurrentAccount class write test suite class