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

Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

Embed Size (px)

Citation preview

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

Exercises :Testing Java with JUnit

Alessandro Marchetto

FBK - IRST

Page 2: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

1) Implement and test the “+” operator for pair of numbers2) Extend the “Add” class for working with not a not fixed/limitedset of numbers

3) Implement and test other mathematical operators (e.g., “-”, “*”, “/”, “sqrt”, “power”)

4) Test the CurrentAccount class4b) Implement a change requirement into CurrentAccount

5) Write test suites

… all the test cases must be written by using JUnit...

Exercises with JUnit:

Page 3: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

- 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

Page 4: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

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 sametype 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 @RunWith and @Parameters)

1) Implement and test the “+” operator for pairs of numbers (2)

Page 5: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

… 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

2) Extend the “Sum” class for working with a not limited/fixed set of numbers

Page 6: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

… 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”)

Page 7: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

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<account.length; i++) {

result = result + account[i];

}

return result;

}

public static void main(String args[]) {

CurrentAccount c = new CurrentAccount();

c.deposit(10);

}

}

- Given the following CurrentAccount class:

Page 8: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

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 areRevealed

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

Page 9: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

4) Test the CurrentAccount class

- verify that your test suite include at least the following cases:

* test cases for testing just the deposit operationit must be possible to deposit positive amount of moneyit 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 operationit depends on the other operationscall 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)

Page 10: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

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 listit 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 classInitialize the list with 3 account class (with respectively 10, 20, 30 as initial deposit)Test each method of the classConstraints 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

Page 11: Exercises : Testing Java with JUnit Alessandro Marchetto FBK - IRST

5) Write test suites

- For all test cases realted to mathematical operators and for the CurrentAccount class write test suite class