新手求助,junit问题,请高手指点

07-08-27 javaGhost

我照着例子练习使用junit,有个问题不只怎么回事,请高手指点

-------Money.java代码

public class Money{

private int fAmount;

private String fCurrency;

public Money(int amount, String currency){

fAmount = amount;

fCurrency = currency;

}

public int getAmount(){

return fAmount;

}

public String getCurrency(){

return fCurrency;

}

public boolean equals(Object anObject){

if(anObject instanceof Money){

Money aMoney = (Money)anObject;

return aMoney.getCurrency().equals(getCurrency()) && getAmount() == aMoney.getAmount();

}

return false;

}

public String toString(){

return String.valueOf(getAmount()) + "-" + getCurrency();

}

};

------MoneyBusiness.java 代码

public class MoneyBusiness{

public static Money add(Money m1,Money m2){

return new Money(m1.getAmount()+m2.getAmount(),m1.getCurrency());

}

public static Money add(int m1,int m2){

return new Money(m1+m2,"CHF");

}

public static void main(String[] args){

Money m12CHF = new Money(12,"CHF");

Money m14CHF = new Money(14,"CHF");

Money m = add(m12CHF,m14CHF);

System.out.println("add result: " + m.toString());

}

}

------MoneyBusinessTest.java代码

import junit.framework.TestCase;

import junit.framework.Assert;

public class MoneyBusinessTest extends TestCase {

private Money m12CHF;

private Money m14CHF;

protected void setUP(){

m12CHF = new Money(12,"CHF");

m14CHF = new Money(14,"CHF");

}

public void testAdd(){

Money expected = new Money(26,"CHF");

//Money result = new Money(26,"CHF");

Money result = MoneyBusiness.add(m12CHF,m14CHF);

Assert.assertTrue("***should be ture***",expected.equals(result));

}

}

--------------

运行MoneyBusiness.main方法没问题

可用junit测试通不过,显示:

java.lang.NullPointerException

at MoneyBusiness.add(MoneyBusiness.java:6)

at MoneyBusinessTest.testAdd(MoneyBusinessTest.java:16)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at junit.framework.TestCase.runTest(TestCase.java:168)

at junit.framework.TestCase.runBare(TestCase.java:134)

at junit.framework.TestResult$1.protect(TestResult.java:110)

at junit.framework.TestResult.runProtected(TestResult.java:128)

at junit.framework.TestResult.run(TestResult.java:113)

at junit.framework.TestCase.run(TestCase.java:124)

at junit.framework.TestSuite.runTest(TestSuite.java:232)

at junit.framework.TestSuite.run(TestSuite.java:227)

at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)

at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)

at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

请高手指点,不甚感激

javaGhost
2007-08-27 16:11

其实很简单:

一个javabean: Money.java

一个业务: MoneyBusiness.java

一个方法:MoneyBusiness.add(Money m1, Money2)

用junit测试add方法

javaGhost
2007-08-28 11:35

怎么没人应啊! benq帮帮我吧

hellohong
2007-08-28 12:34

你的测试用例的大小写错了,

protected void setUP(){....}

这里的setUP应该改为setUp(), 是字母p小写噢, 这样的话其才是继承父类的一个方法.

javaGhost
2007-08-28 17:27

唉,丢人哟

这么简单的问题,害我折腾了几天

2Go 1 2 下一页