Exercises Testing Java with JUnit Alessandro Marchetto FBK
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 Current. Account class 4 b) Implement a change requirement into Current. Account 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 “Math. Operators 2”) - 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 “j. Unit. Test_X” - Where “_X” could be equal to “ 3 x” or “ 4 x” 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: 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 (use @Run. With and @Parameters)
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 “Add. Extended” 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 @Run. With 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. , “-”, “*”, “/”, “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 Current. Account class - Given the following Current. Account class: class Current. Account { int account[]; int last. Move; Current. Account() { last. Move = 0; account = new int[10]; } public void deposit(int value) { account[last. Move] = value; last. Move++; } public void draw(int value) { account[last. Move] = value; last. Move++; } …… …. 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[]) { Current. Account c = new Current. Account(); c. deposit(10); } }
4) Test the Current. Account class - Create a new eclipse project called “Current. Account” - Create the class “Current. Account” in a package “bank. Account” - 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 Current. Account 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 depositive 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 @Run. With and @Parameters)
4 b) Implement a change requirement into Current. Account - Implement and test the following change requirement: * Add a class called “Collection. Of. Accounts” it contains a list of accounts (“Linked. List<Current. Account>”) 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<Current. Account>” * 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 @Run. With 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 Current. Account class write test suite class
- Slides: 11