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 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
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....
A researcher tests whether 4 treatments for depression are different, and gets a significant value for...
A researcher tests whether 4 treatments for depression are different, and gets a significant value for F. What can the researcher conclude about differences between groups? What can the researcher not conclude about differences between groups? 

Which of the following is not required to be disclosed? Significant account policies Guaranteed debt Five...
Which of the following is not required to be disclosed? Significant account policies Guaranteed debt Five year projections Nature of business Net sales
Question - Write a Client class with a main method that tests the data structures as...
Question - Write a Client class with a main method that tests the data structures as follows: For the ArrayStack, LinkedStack, ArrayQueue, LinkedQueue, and ArrayList: Perform a timing test for each of these data structures. Each timing test should measure in nanoseconds how long it takes to add and remove N Integers from the structure. N should vary from 10 to 1,000,000,000 increasing N by a factor of 10 for each test. Depending on your system you may run out...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT