Re: JUNIT questions
- From: "Taria" <mchew02@xxxxxxxxxxx>
- Date: 28 Feb 2007 13:44:47 -0800
With what I know now in hand from previous posts, I go back to my
JUNIT code, hit run and a red bar appears(denoting failed methods) and
the list that goes with it.
My JUNIT program is built as a result from trying to teach myself how
to write a simple JUNIT program from a tutorial. Their JUNIT program
is almost identical to mines, when I replace my code with theirs, they
get a green bar, my JUNIT code gets a red one. :x (I'm so special!
lol) But what this tells me is that the JUNIT code is faulty not
the class I'm testing. Ok, fine, let's go from there.
I tried to compare the tutorial code with mines and my code is almost
identical. I have different values to the initialization routine
which shouldn't matter. Other than that..I don't know why I'm getting
a red bar.
Here is the class tested with JUNIT code:
public class Money {
int dollars;
int cents;
String name;
public Money(int dollars, int cents, String name) {
this.dollars = dollars;
this.cents = cents;
this.name = name;
}
public int getDollars() {
return this.dollars;
}
public int getCents() {
return this.cents;
}
public String getName() {
return this.name;
}
public Money add(Money money2) {
int sumDollars = this.dollars + money2.getDollars();
int sumCents = this.cents + money2.getCents();
return new Money(sumDollars, sumCents, this.name);
}
public static void main(String[] args) {
Money money1 = new Money(100, 46, "USD");
System.out.println("Dollars = " + money1.getDollars() + ", Cents =
" + money1.getCents() +
", Name = " + money1.getName());
}
}
And now my JUNIT code:
import junit.framework.TestCase;
public class MoneyTest extends TestCase {
private Money testMoney;
protected void setUp() throws Exception {
super.setUp();
Money testMoney = new Money (5, 7, "USD");
}
public void testGetDollars() {
assertEquals("Testing GetDollars:",5,this.testMoney.getDollars());
}
public void testGetCents() {
assertEquals("Testing GetCents:",7,this.testMoney.getCents());
}
public void testGetName() {
assertEquals ("Testing GetName: ","USD",this.testMoney.getName());
}
public void testAdd() {
Money testMoney2 = new Money (150,3,"USD");
//do the operation add
Money sum = this.testMoney.add(testMoney2);
assertEquals ("Testing Adding of Dollars: ",155,sum.getDollars());
assertEquals ("Testing Adding of Cents: ",10,sum.getCents());
}
}
I realize that writing JUNIT code for accessors is not really needed
but I did them as an exercise to get more familiar with JUNIT. Also,
I noted I named my JUNIT program the wrong way, it should be
"TestMoney" not "MoneyTest." Forgive me! Lol, my very first virgin
JUNIT program. *hides*
Anyhow, any thoughts appreciated!
.
- Follow-Ups:
- Re: JUNIT questions
- From: Taria
- Re: JUNIT questions
- References:
- JUNIT questions
- From: Taria
- Re: JUNIT questions
- From: Mark Jeffcoat
- Re: JUNIT questions
- From: andrewmcdonagh
- JUNIT questions
- Prev by Date: Re: need java trainer
- Next by Date: Re: JUNIT questions
- Previous by thread: Re: JUNIT questions
- Next by thread: Re: JUNIT questions
- Index(es):