In: Computer Science
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 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
/////////////////////////////////////////////////////////////////
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());
}
}
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////