• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Junit test cases

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
     
    Vani Shastri
    Ranch Hand
    Posts: 52
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic