- 浏览: 158722 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
jiang5769156:
问题解决,求原因
Failed creating java D:\jre6\bin\client\jvm.dll的解决 -
hite:
是能解决问题,同问 原因
Failed creating java D:\jre6\bin\client\jvm.dll的解决 -
lyhsmile:
luckybat 写道工厂方法...针对的是有多个产品的情形
...
java工厂模式的简介 -
xiaoqing20:
或者他的方法有用
但是我这样做了 但是没有反应 还是不行
后来 ...
Failed creating java D:\jre6\bin\client\jvm.dll的解决 -
ileson:
Java远程通讯可选技术及原理
很久前就接触了JUNIT,但是没有真正的使用它,今天又重温了一下,做个笔记,留个痕迹,不至于忘得太彻底,无从查证。
在Eclipse中使用Junit的步骤:
1.在ECLIPSE菜单上单击”窗口“--“首选项”---JAVA ---构建路径----类路径变量---新建中加入junit.jar(在ELIPSE的PLUGINS中:plugins/org.JUnit_3.8.1).
2.如果需要可以把JUnit的愿代码添加。路径(/plugins/org.eclipse.jdt.sortce_3.12/org.junit_3.81/junitsrc.zip)
3.创建项目:名字叫做JUnitTest,加入调试需要的.jar文件。
4.建立一个文件夹:junit.samples.money,把要被调试的程序放入里面(一般是把测试用例和被测试类放在同一个包中的,但是为了区分,加强体系,我们建立两个包,一个放被测试类,一个放测试类)
被测试类:
package junit.samples.money;
/**
* The common interface for simple Monies and MoneyBags
*
*/
public interface IMoney {
/**
* Adds a money to this money.
*/
public abstract IMoney add(IMoney m);
/**
* Adds a simple Money to this money. This is a helper method for
* implementing double dispatch
*/
public abstract IMoney addMoney(Money m);
/**
* Adds a MoneyBag to this money. This is a helper method for
* implementing double dispatch
*/
public abstract IMoney addMoneyBag(MoneyBag s);
/**
* Tests whether this money is zero
*/
public abstract boolean isZero();
/**
* Multiplies a money by the given factor.
*/
public abstract IMoney multiply(int factor);
/**
* Negates this money.
*/
public abstract IMoney negate();
/**
* Subtracts a money from this money.
*/
public abstract IMoney subtract(IMoney m);
/**
* Append this to a MoneyBag m.
*/
public abstract void appendTo(MoneyBag m);
}
package junit.samples.money;
/**
* A simple Money.
*
*/
public class Money implements IMoney {
private int fAmount;
private String fCurrency;
/**
* Constructs a money from the given amount and currency.
*/
public Money(int amount, String currency) {
fAmount= amount;
fCurrency= currency;
}
/**
* Adds a money to this money. Forwards the request to the addMoney helper.
*/
public IMoney add(IMoney m) {
return m.addMoney(this);
}
public IMoney addMoney(Money m) {
if (m.currency().equals(currency()) )
return new Money(amount()+m.amount(), currency());
return MoneyBag.create(this, m);
}
public IMoney addMoneyBag(MoneyBag s) {
return s.addMoney(this);
}
public int amount() {
return fAmount;
}
public String currency() {
return fCurrency;
}
public boolean equals(Object anObject) {
if (isZero())
if (anObject instanceof IMoney)
return ((IMoney)anObject).isZero();
if (anObject instanceof Money) {
Money aMoney= (Money)anObject;
return aMoney.currency().equals(currency())
&& amount() == aMoney.amount();
}
return false;
}
public int hashCode() {
return fCurrency.hashCode()+fAmount;
}
public boolean isZero() {
return amount() == 0;
}
public IMoney multiply(int factor) {
return new Money(amount()*factor, currency());
}
public IMoney negate() {
return new Money(-amount(), currency());
}
public IMoney subtract(IMoney m) {
return add(m.negate());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("["+amount()+" "+currency()+"]");
return buffer.toString();
}
public /*this makes no sense*/ void appendTo(MoneyBag m) {
m.appendMoney(this);
}
}
package junit.samples.money;
import java.util.Enumeration;
import java.util.Vector;
/**
* A MoneyBag defers exchange rate conversions. For example adding
* 12 Swiss Francs to 14 US Dollars is represented as a bag
* containing the two Monies 12 CHF and 14 USD. Adding another
* 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to
* the deferred exchange rate conversion we can later value a
* MoneyBag with different exchange rates.
*
* A MoneyBag is represented as a list of Monies and provides
* different constructors to create a MoneyBag.
*/
public class MoneyBag implements IMoney {
private Vector fMonies= new Vector(5);
public static IMoney create(IMoney m1, IMoney m2) {
MoneyBag result= new MoneyBag();
m1.appendTo(result);
m2.appendTo(result);
return result.simplify();
}
public IMoney add(IMoney m) {
return m.addMoneyBag(this);
}
public IMoney addMoney(Money m) {
return MoneyBag.create(m, this);
}
public IMoney addMoneyBag(MoneyBag s) {
return MoneyBag.create(s, this);
}
void appendBag(MoneyBag aBag) {
for (Enumeration e= aBag.fMonies.elements(); e.hasMoreElements(); )
appendMoney((Money)e.nextElement());
}
void appendMoney(Money aMoney) {
if (aMoney.isZero()) return;
IMoney old= findMoney(aMoney.currency());
if (old == null) {
fMonies.addElement(aMoney);
return;
}
fMonies.removeElement(old);
IMoney sum= old.add(aMoney);
if (sum.isZero())
return;
fMonies.addElement(sum);
}
public boolean equals(Object anObject) {
if (isZero())
if (anObject instanceof IMoney)
return ((IMoney)anObject).isZero();
if (anObject instanceof MoneyBag) {
MoneyBag aMoneyBag= (MoneyBag)anObject;
if (aMoneyBag.fMonies.size() != fMonies.size())
return false;
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
if (!aMoneyBag.contains(m))
return false;
}
return true;
}
return false;
}
private Money findMoney(String currency) {
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
if (m.currency().equals(currency))
return m;
}
return null;
}
private boolean contains(Money m) {
Money found= findMoney(m.currency());
if (found == null) return false;
return found.amount() == m.amount();
}
public int hashCode() {
int hash= 0;
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Object m= e.nextElement();
hash^= m.hashCode();
}
return hash;
}
public boolean isZero() {
return fMonies.size() == 0;
}
public IMoney multiply(int factor) {
MoneyBag result= new MoneyBag();
if (factor != 0) {
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
result.appendMoney((Money)m.multiply(factor));
}
}
return result;
}
public IMoney negate() {
MoneyBag result= new MoneyBag();
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
result.appendMoney((Money)m.negate());
}
return result;
}
private IMoney simplify() {
if (fMonies.size() == 1)
return (IMoney)fMonies.elements().nextElement();
return this;
}
public IMoney subtract(IMoney m) {
return add(m.negate());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); )
buffer.append(e.nextElement());
buffer.append("}");
return buffer.toString();
}
public void appendTo(MoneyBag m) {
m.appendBag(this);
}
}
5建立测试用例,名字叫做testpackage,右击”testpackage“----”新建“---"其他"----Java---Junit----Junit测试用例-----下一步---新建JUnit测试用例,选择setup():测试类的初始化,tearDown():被测试类的销毁,再依次处理。按完成后,一个框架就架设好了,你只要忘里面放入测试代码就可以拉。
测试用例:
package testpackage;
import junit.framework.TestCase;
import junit.samples.money.*;
public class MoneyTest2 extends TestCase {
private Money m1;
private Money m2;
private Money m3;
protected void setUp() throws Exception {
super.setUp();
m1=new Money(12,"rmb");
m2=new Money(24,"rmb");
m2=new Money(10,"usd");
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testAddMoney() {
Money temp=new Money(12,"rmb");
assertEquals(m2,m1.addMoney(temp));
}
public void testEqualsObject() {
assertFalse(m3.equals(m1));
}
}
package testpackage;
import junit.framework.TestCase;
import junit.samples.money.*;
public class MoneyTest extends TestCase {
private Money f12CHF;
private Money f14CHF;
private Money f7USD;
private Money f21USD;
private IMoney fMB1;
private IMoney fMB2;
public static void main(String args[]) {
junit.textui.TestRunner.run(MoneyTest.class);
}
protected void setUp() {
f12CHF= new Money(12, "CHF");
f14CHF= new Money(14, "CHF");
f7USD= new Money( 7, "USD");
f21USD= new Money(21, "USD");
fMB1= MoneyBag.create(f12CHF, f7USD);
fMB2= MoneyBag.create(f14CHF, f21USD);
}
public void testBagMultiply() {
// {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}
IMoney expected= MoneyBag.create(new Money(24, "CHF"), new Money(14, "USD"));
assertEquals(expected, fMB1.multiply(2));
assertEquals(fMB1, fMB1.multiply(1));
assertTrue(fMB1.multiply(0).isZero());
}
public void testBagNegate() {
// {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}
IMoney expected= MoneyBag.create(new Money(-12, "CHF"), new Money(-7, "USD"));
assertEquals(expected, fMB1.negate());
}
public void testBagSimpleAdd() {
// {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
assertEquals(expected, fMB1.add(f14CHF));
}
public void testBagSubtract() {
// {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}
IMoney expected= MoneyBag.create(new Money(-2, "CHF"), new Money(-14, "USD"));
assertEquals(expected, fMB1.subtract(fMB2));
}
public void testBagSumAdd() {
// {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(28, "USD"));
assertEquals(expected, fMB1.add(fMB2));
}
public void testIsZero() {
assertTrue(fMB1.subtract(fMB1).isZero());
assertTrue(MoneyBag.create(new Money (0, "CHF"), new Money (0, "USD")).isZero());
}
public void testMixedSimpleAdd() {
// [12 CHF] + [7 USD] == {[12 CHF][7 USD]}
IMoney expected= MoneyBag.create(f12CHF, f7USD);
assertEquals(expected, f12CHF.add(f7USD));
}
public void testBagNotEquals() {
IMoney bag= MoneyBag.create(f12CHF, f7USD);
assertFalse(bag.equals(new Money(12, "DEM").add(f7USD)));
}
public void testMoneyBagEquals() {
assertTrue(!fMB1.equals(null));
assertEquals(fMB1, fMB1);
IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
assertTrue(fMB1.equals(equal));
assertTrue(!fMB1.equals(f12CHF));
assertTrue(!f12CHF.equals(fMB1));
assertTrue(!fMB1.equals(fMB2));
}
public void testMoneyBagHash() {
IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
assertEquals(fMB1.hashCode(), equal.hashCode());
}
public void testMoneyEquals() {
assertTrue(!f12CHF.equals(null));
Money equalMoney= new Money(12, "CHF");
assertEquals(f12CHF, f12CHF);
assertEquals(f12CHF, equalMoney);
assertEquals(f12CHF.hashCode(), equalMoney.hashCode());
assertTrue(!f12CHF.equals(f14CHF));
}
public void testMoneyHash() {
assertTrue(!f12CHF.equals(null));
Money equal= new Money(12, "CHF");
assertEquals(f12CHF.hashCode(), equal.hashCode());
}
public void testSimplify() {
IMoney money= MoneyBag.create(new Money(26, "CHF"), new Money(28, "CHF"));
assertEquals(new Money(54, "CHF"), money);
}
public void testNormalize2() {
// {[12 CHF][7 USD]} - [12 CHF] == [7 USD]
Money expected= new Money(7, "USD");
assertEquals(expected, fMB1.subtract(f12CHF));
}
public void testNormalize3() {
// {[12 CHF][7 USD]} - {[12 CHF][3 USD]} == [4 USD]
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
Money expected= new Money(4, "USD");
assertEquals(expected, fMB1.subtract(ms1));
}
public void testNormalize4() {
// [12 CHF] - {[12 CHF][3 USD]} == [-3 USD]
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
Money expected= new Money(-3, "USD");
assertEquals(expected, f12CHF.subtract(ms1));
}
public void testPrint() {
assertEquals("[12 CHF]", f12CHF.toString());
}
public void testSimpleAdd() {
// [12 CHF] + [14 CHF] == [26 CHF]
Money expected= new Money(26, "CHF");
assertEquals(expected, f12CHF.add(f14CHF));
}
public void testSimpleBagAdd() {
// [14 CHF] + {[12 CHF][7 USD]} == {[26 CHF][7 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
assertEquals(expected, f14CHF.add(fMB1));
}
public void testSimpleMultiply() {
// [14 CHF] *2 == [28 CHF]
Money expected= new Money(28, "CHF");
assertEquals(expected, f14CHF.multiply(2));
}
public void testSimpleNegate() {
// [14 CHF] negate == [-14 CHF]
Money expected= new Money(-14, "CHF");
assertEquals(expected, f14CHF.negate());
}
public void testSimpleSubtract() {
// [14 CHF] - [12 CHF] == [2 CHF]
Money expected= new Money(2, "CHF");
assertEquals(expected, f14CHF.subtract(f12CHF));
}
}
6.点击运行--运行Junit Test,若无错,则左边的条子为绿,有错则为红,且有错误的提示,有错的测试类才是好的测试类。
来一起运行。
7.创建测试套件:测试套件可运行多个测试案例或测试套件,测试套件可将多个测试案例组合起
测试套件:
package testpackage;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for testpackage");
//$JUnit-BEGIN$
suite.addTestSuite(MoneyTest.class);
suite.addTestSuite(MoneyTest2.class);
//$JUnit-END$
return suite;
}
}
7.创建测试套件:测试套件可运行多个测试案例或测试套件,测试套件可将多个测试案例组合起
在Eclipse中使用Junit的步骤:
1.在ECLIPSE菜单上单击”窗口“--“首选项”---JAVA ---构建路径----类路径变量---新建中加入junit.jar(在ELIPSE的PLUGINS中:plugins/org.JUnit_3.8.1).
2.如果需要可以把JUnit的愿代码添加。路径(/plugins/org.eclipse.jdt.sortce_3.12/org.junit_3.81/junitsrc.zip)
3.创建项目:名字叫做JUnitTest,加入调试需要的.jar文件。
4.建立一个文件夹:junit.samples.money,把要被调试的程序放入里面(一般是把测试用例和被测试类放在同一个包中的,但是为了区分,加强体系,我们建立两个包,一个放被测试类,一个放测试类)
被测试类:
package junit.samples.money;
/**
* The common interface for simple Monies and MoneyBags
*
*/
public interface IMoney {
/**
* Adds a money to this money.
*/
public abstract IMoney add(IMoney m);
/**
* Adds a simple Money to this money. This is a helper method for
* implementing double dispatch
*/
public abstract IMoney addMoney(Money m);
/**
* Adds a MoneyBag to this money. This is a helper method for
* implementing double dispatch
*/
public abstract IMoney addMoneyBag(MoneyBag s);
/**
* Tests whether this money is zero
*/
public abstract boolean isZero();
/**
* Multiplies a money by the given factor.
*/
public abstract IMoney multiply(int factor);
/**
* Negates this money.
*/
public abstract IMoney negate();
/**
* Subtracts a money from this money.
*/
public abstract IMoney subtract(IMoney m);
/**
* Append this to a MoneyBag m.
*/
public abstract void appendTo(MoneyBag m);
}
package junit.samples.money;
/**
* A simple Money.
*
*/
public class Money implements IMoney {
private int fAmount;
private String fCurrency;
/**
* Constructs a money from the given amount and currency.
*/
public Money(int amount, String currency) {
fAmount= amount;
fCurrency= currency;
}
/**
* Adds a money to this money. Forwards the request to the addMoney helper.
*/
public IMoney add(IMoney m) {
return m.addMoney(this);
}
public IMoney addMoney(Money m) {
if (m.currency().equals(currency()) )
return new Money(amount()+m.amount(), currency());
return MoneyBag.create(this, m);
}
public IMoney addMoneyBag(MoneyBag s) {
return s.addMoney(this);
}
public int amount() {
return fAmount;
}
public String currency() {
return fCurrency;
}
public boolean equals(Object anObject) {
if (isZero())
if (anObject instanceof IMoney)
return ((IMoney)anObject).isZero();
if (anObject instanceof Money) {
Money aMoney= (Money)anObject;
return aMoney.currency().equals(currency())
&& amount() == aMoney.amount();
}
return false;
}
public int hashCode() {
return fCurrency.hashCode()+fAmount;
}
public boolean isZero() {
return amount() == 0;
}
public IMoney multiply(int factor) {
return new Money(amount()*factor, currency());
}
public IMoney negate() {
return new Money(-amount(), currency());
}
public IMoney subtract(IMoney m) {
return add(m.negate());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("["+amount()+" "+currency()+"]");
return buffer.toString();
}
public /*this makes no sense*/ void appendTo(MoneyBag m) {
m.appendMoney(this);
}
}
package junit.samples.money;
import java.util.Enumeration;
import java.util.Vector;
/**
* A MoneyBag defers exchange rate conversions. For example adding
* 12 Swiss Francs to 14 US Dollars is represented as a bag
* containing the two Monies 12 CHF and 14 USD. Adding another
* 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to
* the deferred exchange rate conversion we can later value a
* MoneyBag with different exchange rates.
*
* A MoneyBag is represented as a list of Monies and provides
* different constructors to create a MoneyBag.
*/
public class MoneyBag implements IMoney {
private Vector fMonies= new Vector(5);
public static IMoney create(IMoney m1, IMoney m2) {
MoneyBag result= new MoneyBag();
m1.appendTo(result);
m2.appendTo(result);
return result.simplify();
}
public IMoney add(IMoney m) {
return m.addMoneyBag(this);
}
public IMoney addMoney(Money m) {
return MoneyBag.create(m, this);
}
public IMoney addMoneyBag(MoneyBag s) {
return MoneyBag.create(s, this);
}
void appendBag(MoneyBag aBag) {
for (Enumeration e= aBag.fMonies.elements(); e.hasMoreElements(); )
appendMoney((Money)e.nextElement());
}
void appendMoney(Money aMoney) {
if (aMoney.isZero()) return;
IMoney old= findMoney(aMoney.currency());
if (old == null) {
fMonies.addElement(aMoney);
return;
}
fMonies.removeElement(old);
IMoney sum= old.add(aMoney);
if (sum.isZero())
return;
fMonies.addElement(sum);
}
public boolean equals(Object anObject) {
if (isZero())
if (anObject instanceof IMoney)
return ((IMoney)anObject).isZero();
if (anObject instanceof MoneyBag) {
MoneyBag aMoneyBag= (MoneyBag)anObject;
if (aMoneyBag.fMonies.size() != fMonies.size())
return false;
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
if (!aMoneyBag.contains(m))
return false;
}
return true;
}
return false;
}
private Money findMoney(String currency) {
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
if (m.currency().equals(currency))
return m;
}
return null;
}
private boolean contains(Money m) {
Money found= findMoney(m.currency());
if (found == null) return false;
return found.amount() == m.amount();
}
public int hashCode() {
int hash= 0;
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Object m= e.nextElement();
hash^= m.hashCode();
}
return hash;
}
public boolean isZero() {
return fMonies.size() == 0;
}
public IMoney multiply(int factor) {
MoneyBag result= new MoneyBag();
if (factor != 0) {
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
result.appendMoney((Money)m.multiply(factor));
}
}
return result;
}
public IMoney negate() {
MoneyBag result= new MoneyBag();
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); ) {
Money m= (Money) e.nextElement();
result.appendMoney((Money)m.negate());
}
return result;
}
private IMoney simplify() {
if (fMonies.size() == 1)
return (IMoney)fMonies.elements().nextElement();
return this;
}
public IMoney subtract(IMoney m) {
return add(m.negate());
}
public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
for (Enumeration e= fMonies.elements(); e.hasMoreElements(); )
buffer.append(e.nextElement());
buffer.append("}");
return buffer.toString();
}
public void appendTo(MoneyBag m) {
m.appendBag(this);
}
}
5建立测试用例,名字叫做testpackage,右击”testpackage“----”新建“---"其他"----Java---Junit----Junit测试用例-----下一步---新建JUnit测试用例,选择setup():测试类的初始化,tearDown():被测试类的销毁,再依次处理。按完成后,一个框架就架设好了,你只要忘里面放入测试代码就可以拉。
测试用例:
package testpackage;
import junit.framework.TestCase;
import junit.samples.money.*;
public class MoneyTest2 extends TestCase {
private Money m1;
private Money m2;
private Money m3;
protected void setUp() throws Exception {
super.setUp();
m1=new Money(12,"rmb");
m2=new Money(24,"rmb");
m2=new Money(10,"usd");
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testAddMoney() {
Money temp=new Money(12,"rmb");
assertEquals(m2,m1.addMoney(temp));
}
public void testEqualsObject() {
assertFalse(m3.equals(m1));
}
}
package testpackage;
import junit.framework.TestCase;
import junit.samples.money.*;
public class MoneyTest extends TestCase {
private Money f12CHF;
private Money f14CHF;
private Money f7USD;
private Money f21USD;
private IMoney fMB1;
private IMoney fMB2;
public static void main(String args[]) {
junit.textui.TestRunner.run(MoneyTest.class);
}
protected void setUp() {
f12CHF= new Money(12, "CHF");
f14CHF= new Money(14, "CHF");
f7USD= new Money( 7, "USD");
f21USD= new Money(21, "USD");
fMB1= MoneyBag.create(f12CHF, f7USD);
fMB2= MoneyBag.create(f14CHF, f21USD);
}
public void testBagMultiply() {
// {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}
IMoney expected= MoneyBag.create(new Money(24, "CHF"), new Money(14, "USD"));
assertEquals(expected, fMB1.multiply(2));
assertEquals(fMB1, fMB1.multiply(1));
assertTrue(fMB1.multiply(0).isZero());
}
public void testBagNegate() {
// {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}
IMoney expected= MoneyBag.create(new Money(-12, "CHF"), new Money(-7, "USD"));
assertEquals(expected, fMB1.negate());
}
public void testBagSimpleAdd() {
// {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
assertEquals(expected, fMB1.add(f14CHF));
}
public void testBagSubtract() {
// {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}
IMoney expected= MoneyBag.create(new Money(-2, "CHF"), new Money(-14, "USD"));
assertEquals(expected, fMB1.subtract(fMB2));
}
public void testBagSumAdd() {
// {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(28, "USD"));
assertEquals(expected, fMB1.add(fMB2));
}
public void testIsZero() {
assertTrue(fMB1.subtract(fMB1).isZero());
assertTrue(MoneyBag.create(new Money (0, "CHF"), new Money (0, "USD")).isZero());
}
public void testMixedSimpleAdd() {
// [12 CHF] + [7 USD] == {[12 CHF][7 USD]}
IMoney expected= MoneyBag.create(f12CHF, f7USD);
assertEquals(expected, f12CHF.add(f7USD));
}
public void testBagNotEquals() {
IMoney bag= MoneyBag.create(f12CHF, f7USD);
assertFalse(bag.equals(new Money(12, "DEM").add(f7USD)));
}
public void testMoneyBagEquals() {
assertTrue(!fMB1.equals(null));
assertEquals(fMB1, fMB1);
IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
assertTrue(fMB1.equals(equal));
assertTrue(!fMB1.equals(f12CHF));
assertTrue(!f12CHF.equals(fMB1));
assertTrue(!fMB1.equals(fMB2));
}
public void testMoneyBagHash() {
IMoney equal= MoneyBag.create(new Money(12, "CHF"), new Money(7, "USD"));
assertEquals(fMB1.hashCode(), equal.hashCode());
}
public void testMoneyEquals() {
assertTrue(!f12CHF.equals(null));
Money equalMoney= new Money(12, "CHF");
assertEquals(f12CHF, f12CHF);
assertEquals(f12CHF, equalMoney);
assertEquals(f12CHF.hashCode(), equalMoney.hashCode());
assertTrue(!f12CHF.equals(f14CHF));
}
public void testMoneyHash() {
assertTrue(!f12CHF.equals(null));
Money equal= new Money(12, "CHF");
assertEquals(f12CHF.hashCode(), equal.hashCode());
}
public void testSimplify() {
IMoney money= MoneyBag.create(new Money(26, "CHF"), new Money(28, "CHF"));
assertEquals(new Money(54, "CHF"), money);
}
public void testNormalize2() {
// {[12 CHF][7 USD]} - [12 CHF] == [7 USD]
Money expected= new Money(7, "USD");
assertEquals(expected, fMB1.subtract(f12CHF));
}
public void testNormalize3() {
// {[12 CHF][7 USD]} - {[12 CHF][3 USD]} == [4 USD]
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
Money expected= new Money(4, "USD");
assertEquals(expected, fMB1.subtract(ms1));
}
public void testNormalize4() {
// [12 CHF] - {[12 CHF][3 USD]} == [-3 USD]
IMoney ms1= MoneyBag.create(new Money(12, "CHF"), new Money(3, "USD"));
Money expected= new Money(-3, "USD");
assertEquals(expected, f12CHF.subtract(ms1));
}
public void testPrint() {
assertEquals("[12 CHF]", f12CHF.toString());
}
public void testSimpleAdd() {
// [12 CHF] + [14 CHF] == [26 CHF]
Money expected= new Money(26, "CHF");
assertEquals(expected, f12CHF.add(f14CHF));
}
public void testSimpleBagAdd() {
// [14 CHF] + {[12 CHF][7 USD]} == {[26 CHF][7 USD]}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));
assertEquals(expected, f14CHF.add(fMB1));
}
public void testSimpleMultiply() {
// [14 CHF] *2 == [28 CHF]
Money expected= new Money(28, "CHF");
assertEquals(expected, f14CHF.multiply(2));
}
public void testSimpleNegate() {
// [14 CHF] negate == [-14 CHF]
Money expected= new Money(-14, "CHF");
assertEquals(expected, f14CHF.negate());
}
public void testSimpleSubtract() {
// [14 CHF] - [12 CHF] == [2 CHF]
Money expected= new Money(2, "CHF");
assertEquals(expected, f14CHF.subtract(f12CHF));
}
}
6.点击运行--运行Junit Test,若无错,则左边的条子为绿,有错则为红,且有错误的提示,有错的测试类才是好的测试类。
来一起运行。
7.创建测试套件:测试套件可运行多个测试案例或测试套件,测试套件可将多个测试案例组合起
测试套件:
package testpackage;
import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for testpackage");
//$JUnit-BEGIN$
suite.addTestSuite(MoneyTest.class);
suite.addTestSuite(MoneyTest2.class);
//$JUnit-END$
return suite;
}
}
7.创建测试套件:测试套件可运行多个测试案例或测试套件,测试套件可将多个测试案例组合起
发表评论
-
用smartupload下载excel,excel全为乱码!
2009-05-11 11:57 1572问题描叙:可以下载文件,文件名名不为中文,但是下载的后的exc ... -
tomcat可以启动,但是到不了欢迎界面,怎么解决?
2009-03-13 12:46 4904tomcat可以启动,但是到不了欢迎界面,怎么解决? 日志: ... -
Failed creating java D:\jre6\bin\client\jvm.dll的解决
2009-03-13 11:54 15893启动tomcat服务时出现错误Failed creating ... -
什么是缓存(cache)
2008-07-16 20:56 1229操作系统的任务主要是 ... -
提高自己JAVA水平的十大技术讨论
2008-07-14 20:01 1096提高自己JAVA水平的十大 ... -
对java再认识
2008-05-26 00:00 1330最近看了一些关于JAVA的文章,对JAVA有一些新的认 ... -
1、Transaction Script(事务脚本)
2008-05-24 19:14 1417Most business applications ... -
完全解决VMware虚拟机下安装linux上网
2008-05-14 13:48 2806完全解决VMware虚拟 ... -
RedHat Linux 9.0 安装教程 全程图解
2008-05-14 00:34 2263基于人民对LINUX的痴迷和热爱,我今天也装了一个LI ... -
ArrayList和LinkedList的区别是什么?举例说明LinkedList可实现的功能(摘)
2008-05-05 17:38 2302对于处理一列数据项,Java提供了两个类ArrayList和L ... -
Java多线程程序设计详细解析
2008-05-02 01:19 888一、理解多线程 多线程是这样一种机制,它允许在程序中并发 ... -
Java基础:泛型类型的子类及通配符的使用
2008-05-01 18:15 2073在java语言中,我们 ... -
在 Java 的泛型类型中使用通配符
2008-05-01 17:58 1966在 Java 的泛型类型中使用通配符 Java 从版本5起开 ... -
请教在tomcat里如果设置context片断?
2008-03-01 21:34 1029在C:\Tomcat 6.0\conf\server.xm ... -
MyEclipse + Tomcat + SQLServer开发JSP网站,初级配置及介绍!
2008-02-29 21:01 5848最近在边学jsp边做网站 ... -
数据库-数据类型
2008-02-26 21:55 1342在计算机中数据有两种特征:类型和长度。所谓数据类型就是以 ... -
谁可以告诉我一下analysis services 的下载地址?
2008-02-26 19:30 6180[size=large][/size]在安装 ... -
jsp连接SQLSERVER200的问题,急??
2007-10-03 15:56 1154弄了一天了,还是没搞好,有谁帮我看一下,万分感谢,以下是我 ... -
Mvnforum设计者概述MvnForum论坛的架构和设计(转)
2007-06-22 21:05 2382I will try to describe the f ... -
http://springide.org/updatesite/
2007-06-07 16:36 3996软件配置: · Eclipse eclips ...
相关推荐
Juit In Action 中文版 part1
Juit In Action 中文版 part2
Juit In Action 中文版 part3 包太大了,分3部分
在实际使用中,开发者通常会结合构建工具(如 Maven 或 Gradle)和 IDE(如 Eclipse 或 IntelliJ IDEA)来管理和运行 JUnit 测试。这些工具提供了图形界面,使得编写、运行和查看测试结果变得简单高效。 了解和熟练...
ACM博客ACM JUIT成员撰写的技术博客集。
在Junit中,我们通常使用`assertEquals`或`assertNull`等简单的断言,但Hamcrest则允许更复杂的逻辑表达,如`assertThat(expression, matcher)`,这里的`expression`是被测试的对象,`matcher`则是用来验证对象是否...
JUnit框架是Java编程语言中最广泛使用的单元测试框架,它为开发者提供了一种便捷的方式来编写可重复执行的测试用例,确保代码的质量和稳定性。在JavaScript的开发环境中,虽然JUnit主要用于Java,但通过一些工具和库...
JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了一种方便、高效的方式来验证代码的正确性。这个“junit4 jar完整包”包含了所有你需要进行单元测试的类和接口,使得测试过程变得简单且易于...
1. **创建测试类**:首先,你需要创建一个测试类,这个类通常以`Test`结尾,并且继承自`junit.framework.TestCase`(对于JUnit4)或使用`@org.junit.Test`注解(对于JUnit5)标记测试方法。 2. **测试方法**:测试...
JUnit4是Java编程语言中最广泛使用的单元测试框架之一,它为开发者提供了一种方便、高效的方式来验证代码的正确性。这个“junit4 jar包 Java单元测试框架绿色免费版.zip”文件包含的是JUnit4框架的可执行jar包,用于...
java查看class代码工具jd-gui
需要的知识 软件测试基础理论知识 黑盒,白盒 编程语言基础 C/C++ java python等 ...2)偏白盒的robotium,这家伙号称是黑盒,但是本人不太认同~ 因为使用robotium需要知道package和acitivity这样的细节,所以需
该存储库包含在AIT学生分会(JUIT)进行的2021年计算机视觉新手训练营期间编写和讨论的所有代码 依存关系 :hammer_and_wrench: Python 3.6或更高版本- OpenCV 3.5或更高版本pip install opencv-python 目录 :...