1 / 11

Exercises : Testing Java with JUnit

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

pgenovese
Download Presentation

Exercises : Testing Java with JUnit

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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...

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

  4. 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)

  5. 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

  6. 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”)

  7. 4) Test the CurrentAccount class - Given the following CurrentAccount class:

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

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

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

More Related