Question

In: Computer Science

You are required to write 4 significant JUnit tests for the Account class outlined below taking...

You are required to write 4 significant JUnit tests for the Account class outlined below taking into account the specifications below. Your code must include 3 negative test cases.

Specifications for the Account class:

  • The constructor takes as arguments the name, ID and the initial balance.
  • The withdrawn method allows any specified amount between $20 and $1000 to be withdrawn but the withdrawn amount cannot exceed the current balance.
  • The withdraw method is expected to throw the AmtTooLow exception if the amount specified is below $20 and the AmtTooHigh exception if the amount exceeds $1000.
  • The withdraw method is expected to throw InsuffientBalance if the amount specified exceeds the balance.
// The outline of Account class. You are not required to complete these 

public class Account {
     …
     public Account(String name, String ID, double amt)
     {   ….

     }
     public double withdraw(double amt) throws AmtTooLow, AmtTooHigh, InsuffientBalance
     {  … 




     }
}

You are required to write the methods with the @Before and @Test annotations only. You should not attempt to run these tests (as the Account class is not complete).

// you need not write the include

public class AccountTest {

// Code to be inserted in the box

Solutions

Expert Solution

/////////////////////////////////////////////////////////////////


public class AmtTooLow extends Exception {
   public AmtTooLow() {
       super("Amount is Too Low");
   }
}

/////////////////////////////////////////////////////////////////


public class AmtTooHigh extends Exception {
   public AmtTooHigh() {
       super("Amount is Too High");
   }
}

/////////////////////////////////////////////////////////////////


public class InsuffientBalance extends Exception {
   public InsuffientBalance() {
       super("insufficient balance");
   }
}

/////////////////////////////////////////////////////////////////

public class Account {
   private String name;
   private String ID;
   private double bal;
public Account(String name, String ID, double amt)
{
   this.setName(name);
   this.setID(ID);
   this.bal=amt;
}

public double withdraw(double amt) throws AmtTooLow, AmtTooHigh, InsuffientBalance
{
   if(amt<20) {
       throw new AmtTooLow();
   }
   if(amt>1000) {
       throw new AmtTooHigh();
   }
   if(bal<amt) {
       throw new InsuffientBalance();
   }

   bal-=amt;
   return bal;
}
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getID() {
       return ID;
   }
   public void setID(String iD) {
       ID = iD;
   }
   public double getBalance() {
       return this.bal;
   }
}

/////////////////////////////////////////////////////////////////

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;

class AccountTest {

   @Test
   void testAccount() {
       Account account=new Account("Customer1","ABC112",679.88);
       double bal=account.getBalance();
       assertEquals(bal,679.88);
       try {
           bal=account.withdraw(123);
           assertEquals(bal,556.88);          
           bal=account.withdraw(35);
           assertEquals(bal,521.88);
           bal=account.withdraw(83);
           assertEquals(bal,438.88);
          
       } catch (AmtTooLow | AmtTooHigh | InsuffientBalance e) {
           e.printStackTrace();
       }
      
      
   }

   @Test
   void testAmountLow() {
       Account account=new Account("Customer1","ABC112",679.88);
       Exception exception = assertThrows(Exception.class, () -> account.withdraw(12));
       assertEquals("Amount is Too Low", exception.getMessage());
   }
  
  
   @Test
   void testAmountHigh() {
       Account account=new Account("Customer1","ABC112",679.88);
       Exception exception = assertThrows(Exception.class, () -> account.withdraw(1001));
       assertEquals("Amount is Too High", exception.getMessage());
   }
  
  
   @Test
   void testInsufficientBal() {
       Account account=new Account("Customer1","ABC112",79.88);
       Exception exception = assertThrows(Exception.class, () -> account.withdraw(91));
       assertEquals("insufficient balance", exception.getMessage());
   }
  
}

/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////


Related Solutions

You must write tests for the following: all tests must be written in JUnit 5. Customer...
You must write tests for the following: all tests must be written in JUnit 5. Customer Class Checking that the customer number autoincrements with each new customer object created Order Class Checking that the order number autoincrements with each new order object created Adding a product that is already in the order Removing a product that is in the order - quantity less than the quantity in the order Removing a product that is in the order - quantity that...
You are required to write a `Critical Review` of the two In-Situ Tests written below on...
You are required to write a `Critical Review` of the two In-Situ Tests written below on one Single-Sided WORD DOCUMENT (Not more than one page!): A) Flat Dilatometer Test (DMT) B) Field Vane Shear Test Your `Critical Review` should possess two Sub-Titles (a) `Critical Assumptions related to these Tests`, and (b) `Advantages and Disadvantages of both Tests
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these...
Write a Junit test method that takes 2 Arrays of type Integer[], and tests whether these 2 Arrays are equal or not, and also if the elements are all even numbers. Describe under what conditions these 2 Arrays would be considered equal.
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests...
Program Specifications: Write a program that defines a class HumanBMI, implements it as required, and tests the class implementation. The class definition and implementation should be separated into HumanBMI.h and HumanBMI.cpp files. A. The class HumanBMI consists of three private member variables: name of type string, height of type int in inches, and weight of type int in pounds. The class HumanBMI also includes the following public member functions: 1) setName to set the name member variable with a string...
Express the result, taking into account the following: A three significant figures With the right unit...
Express the result, taking into account the following: A three significant figures With the right unit 1) 50.0 mL of a solution contains 0.0285 moles of AgNO3. Calculate Molarity for a solution 2) A sample of 0.00116 moles of NaCl is placed in a volumetric flask and water is added to dissolve it up to 25.0 mL. Calculate the Molarity of the solution. 3) Determine the molarity for a NaCl solution, whose volume is 0.01006 L and the amount of...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each question using an a=0.05 significance level. In all cases you are expected to use the appropriate inferential statistical test available in the Excel data analysis tool pack. Questions requiring a post hoc means comparison test (ie Scheffe’s or Tukey’s HSD test) are worth 20 marks, otherwise they are worth 10 marks. A researcher is attempting to determine whether significant differences exist between the mean...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each question using an a=0.05 significance level. In all cases you are expected to use the appropriate inferential statistical test available in the Excel data analysis tool pack. Questions requiring a post hoc means comparison test (ie Scheffe’s or Tukey’s HSD test) are worth 20 marks, otherwise they are worth 10 marks. A toxicologist has conducted a study to determine whether a new insecticide will...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each question using an a=0.05 significance level. In all cases you are expected to use the appropriate inferential statistical test available in the Excel data analysis tool pack. Questions requiring a post hoc means comparison test (ie Scheffe’s or Tukey’s HSD test) are worth 20 marks, otherwise they are worth 10 marks. A researcher is attempting to determine whether significant differences exist between the mean...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each...
The following questions require that you conduct appropriate tests of the null hypotheses outlined in each question using an =0.05 significance level. In all cases you are expected to use the appropriate inferential statistical test available in the Excel data analysis tool pack. Questions requiring a post hoc means comparison test (ie Scheffe’s or Tukey’s HSD test) are worth 20 marks, otherwise they are worth 10 marks. A biologist is interested in the nutritional quality of different species of plants...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the...
Write a C++ program (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator functions for id, balance, and annualInterestRate....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT