aspose file tools*
The moose likes Testing and the fly likes Junit test cases Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Junit test cases" Watch "Junit test cases" New topic
Author

Junit test cases

Vani Shastri
Ranch Hand

Joined: Aug 17, 2006
Posts: 52
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.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
It looks like you are confused. I certainly am now I'm especially puzzled by all the fail("not fail").

For me, unit tests have two jobs:

  • Enforce correct program behaviour
  • Explain what correct behavior should be to human readers



  • While your unit tests seem headed toward the first goal, they don't seem to address the second one at all. This may be where your confusion comes in.

    Let's take your "deplosit0" test:



    This test isn't really testing anything. Mostly it just prints some stuff out, which requires a knowledgeable person to interpret the output. I really doubt that if you came back to this project a year later you would remember that a printed balance of 300.00 followed by a "not fail" message indicate that a deposit of zero should throw an exception!

    I would expect to see somthing much more like:



    I have removed the System.out.prints (they don't actually test anything) and replaced them with asserts which will fail if things are wrong.

    I have checked the balance before and after the bad deposit, to make sure that (even if the deposit of zero correctly throws an exception) there are no unwanted side-effects.

    If I were writing this test suite myself, I'd also probably give that 300 a name e.g.



    Does that approach seem any clearer?


    Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
    Vani Shastri
    Ranch Hand

    Joined: Aug 17, 2006
    Posts: 52
    hey thanks.

    Thats was a very clear explaination.
    It makes a lot of sense putting the assert in the catch block.
    Now jUnit testing seems like entering my head.
    Thanks a lot
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Junit test cases