Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST.

Similar presentations


Presentation on theme: "Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST."— Presentation transcript:

1 Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

2 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... Exercises with JUnit:

3 - 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

4 Please, during implementation and testing consider the following additional constraints: 1)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. 2)When writing test cases try to use just one test method per input type (integer, double, long) with different pairs of number as input 1) Implement and test the + operator for pairs of numbers (2)

5 … after the exercises 1 and 2 … - Write a new class called AddExtended that takes a set of numbers (use a Vector 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 - Run the test cases, if there are bugs fix them and re-run until no bugs are in the class 2) Extend the Sum class for working with a not limited/fixed set of numbers

6 … 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) 3) Implement and test other mathematical operators (e.g.,-, *, /, sqrt, power)

7 4) Test the 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) { account[lastMove] = value; lastMove++; } …… …. public int settlement() { int result = 0; for (int i=0; i

8 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

9 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 ( 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

10 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 ) 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

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


Download ppt "Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST."

Similar presentations


Ads by Google