Hi all,
This is a sample code :
package bankaccount;
public class BankAccount {
double balance;
double limit;
double lowestBalance;
public BankAccount(double b, double l, double lb){
balance=b;
limit=l;
lowestBalance=lb;
}
//Withdraw money
public void withdraw(double amount){
//if(amount>limit)throw new IllegalArgumentException();
//if(balance-amount<lowestBalance)throw new IllegalArgumentException();
//if(amount<=0.00)throw new IllegalArgumentException();
//else{
balance=balance-amount;
//}
}
//Deposit money
public void deposit(double amount){
//if(amount<=0.00)throw new IllegalArgumentException();
balance=balance+amount;
}
//Get the value of balance
public double getBalance(){
return balance;
}
}
Now here is the
test case class :
package tests;
import bankaccount.BankAccount;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class BankAccountTest extends TestCase {
public BankAccount ba;
public BankAccountTest(
String name){
super(name);
}
/*
* @see TestCase#setUp()
*/
public void setUp() throws Exception {
ba=new BankAccount(300,100,0);
}
/*
* @see TestCase#tearDown()
*/
public void tearDown() throws Exception {
super.tearDown();
}
/*
* TestCase for testing methods
*/
public void testWithdraw(){
ba.withdraw(0);
System.out.println(ba.getBalance());
assertTrue(ba.getBalance()==300);
}
public void testDeposit(){
ba.deposit(100);
System.out.println(ba.getBalance());
assertTrue(ba.getBalance()==400);
}
public void testLimit(){
try {
ba.withdraw(90);
System.out.println(ba.getBalance());
fail("not failed");
}
catch (IllegalArgumentException e) {
System.out.println("Withdraw failed (limit), Balance is:"+ba.getBalance());
}
}
public void testWithdraw0(){
try {
ba.withdraw(0);
System.out.println(ba.getBalance());
fail ("not failed");
}
catch (IllegalArgumentException e) {
System.out.println("Withdraw failed (0 amount), Balance is:"+ba.getBalance());
}
}
public void testDeposit0(){
try {
ba.deposit(0);
System.out.println(ba.getBalance());
fail("not failed");
}
catch (IllegalArgumentException e) {
System.out.println("Deposit failed (0 amound), Balance is:"+ba.getBalance());
}
}
public void testWithdrawSmaller0(){
try {
ba.withdraw(-10);
System.out.println(ba.getBalance());
fail("not failed");
}
catch (IllegalArgumentException e) {
System.out.println("Withdraw failed (<0 amound), Balance is:"+ba.getBalance());
}
}
public void testDepositSmaller0(){
try {
ba.deposit(-10);
System.out.println(ba.getBalance());
fail("not failed");
}
catch (IllegalArgumentException e) {
System.out.println("Deposit failed (<0 amound), Balance is:"+ba.getBalance());
}
}
public void testLowest(){
try {
ba.withdraw(400);
System.out.println(ba.getBalance());
fail("not failed");
}
catch (IllegalArgumentException e) {
System.out.println("Withdraw failed (lowestbalance), Balance is:"+ba.getBalance());
}
}
}
Now my doubt is, a few testcases like withdraw0 are meant not to occur, ie, no 0 withdrawal can be made. Should we consider this as a testcase passed or failed? In this case the above testcase class fails to run on
jUnit.
I am quite confused. I hope i havent made it confusing for you guys as well.
Kindly help me.
Thanks.