51Testing软件测试网 Ri;yBnkQ!z^
之后,我们创建一个junit测试代码MoneyTest.java,来测试Money.java和MoneyBag.java类,这里我也把它放在src目录下;如果企业应用,你就要把目录分析的详细些,例如src子目录下放所有的源代码,子目录下放所有的junit测试代码:x
tgJ'\0
3GW4cQ}:{~5{FD0import junit.framework.*;51Testing软件测试网5Ec/F3Frd51Testing软件测试网%i%Zz"P{W-r
public class MoneyTest extends TestCase {tE&\^F]"E0
{edUO l4X,|z0private Money f12CHF;^;Pb2g^-v0
s5~s$h6L3\f4TE0private Money f14CHF;C`8\S9`tY0
\la,Q6\-zt8p0private Money f7USD;g xqX5ND'c*`0U051Testing软件测试网%\$I@!drtm$s%@w
private Money f21USD;Lo X8S[h%Au051Testing软件测试网q"OmFD&i4a
HJ:u]NG\0
^ClXI5eO Z0private IMoney fMB1;6l@zE6txnC+f0
1zL'}&u0Ht"O2F-`G],L0private IMoney fMB2;51Testing软件测试网8s7`z p)K5t8D51Testing软件测试网Gf:d$B!p*p
51Testing软件测试网$S9ufT4MV4P5f|7I51Testing软件测试网FQ7l._+Sn/R0WwC
public static void main(String args[]) {51Testing软件测试网0u^1Nm!m51Testing软件测试网rS7k|R
junit.textui.TestRunner.run(MoneyTest.class);51Testing软件测试网#s8wWnH_Q8b
,sH'G%_"{,X[$C0}51Testing软件测试网1E5jmx5B+IdkH
9jOx-[2V"T(C$r0protected void setUp() {6OE!B`3u^ S'D*wV0
:[z!kl`O0f12CHF= new Money(12, "CHF");ly4Z.`h&\7@,T051Testing软件测试网jWbT2hkM
f14CHF= new Money(14, "CHF");51Testing软件测试网u&@+j["Z{}i51Testing软件测试网eCE:TY
f7USD= new Money( 7, "USD");51Testing软件测试网0C2gCs8xm{
n2m"Wh9[4`q0f21USD= new Money(21, "USD");g!?P~$jgj051Testing软件测试网d^$D*Hs
51Testing软件测试网C&D zgFH@z/T
N
Q0kx6w
~0fMB1= MoneyBag.create(f12CHF, f7USD);51Testing软件测试网E3K1t'I;r0`a Q
?`8L!MAn0fMB2= MoneyBag.create(f14CHF, f21USD);51Testing软件测试网5F"r)Go0tLA"|
[
t#A0k)hPQ0}51Testing软件测试网 ?.o1W2D|6zJB!A?c b:r
%_:t5M C2{p%Y4``0K\"x0public void testBagMultiply() {b{J4CQ(CZ0
S rhZQ-J~0// {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}51Testing软件测试网Xt:NOa
jzn0xnp7rz@0IMoney expected= MoneyBag.create(new Money(24, "CHF"), new Money(14, "USD"));4oEa0XY\-i051Testing软件测试网$PPSutc
assertEquals(expected, fMB1.multiply(2));51Testing软件测试网r%`4Nn!hS
/H.kP@e#]/Qu0assertEquals(fMB1, fMB1.multiply(1));8W.NV_8H0
+J0uau$B:w\-x5_ r0assertTrue(fMB1.multiply(0).isZero());j-nt2k)JkN2]0
@Y&[m(e4['U%[0}"Z|#X,m7}7E)Q3O4l6F0
j a8y\5vd0G0public void testBagNegate() {saQy%aE&w0
kOB!Z^g&E0// {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}XIN2GY*`051Testing软件测试网T6^+?E?SQa
IMoney expected= MoneyBag.create(new Money(-12, "CHF"), new Money(-7, "USD"));|*KA:b"]s#^051Testing软件测试网^ENZCHxb
assertEquals(expected, fMB1.negate());qei)td m:hiX051Testing软件测试网(A+l)A'A2bO1pv
}51Testing软件测试网X G)q*vLu
]#Ek@6i0public void testBagSimpleAdd() {51Testing软件测试网 k_a.Q+J;I
%I?
e0]:Tz7}}0// {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}51Testing软件测试网%XA[K$uL+e&Y51Testing软件测试网0ERWv2gt,]C8\:}
IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(7, "USD"));3ikSpk!f0
Nb-R~ ?9~.Bj0assertEquals(expected, fMB1.add(f14CHF));51Testing软件测试网0lFb
MM^$~C\51Testing软件测试网#U6o;b+L0U6VtB6Z
}51Testing软件测试网$BGX2?1a0P
I;P`"B1a"h'iy0public void testBagSubtract() {51Testing软件测试网^+D4^/yi-G7Pu51Testing软件测试网F"l.e;]GL
Q
// {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}mMHXAcOO/b051Testing软件测试网,g7dG!J.Kz0I*}2k
IMoney expected= MoneyBag.create(new Money(-2, "CHF"), new Money(-14, "USD"));;hL*e"J'k0j;}~,cV051Testing软件测试网0HY _|3{y
assertEquals(expected, fMB1.subtract(fMB2));s,y;PA%X3v:J_+^0
K;_8A2L5p.G`0}51Testing软件测试网E`1?+Re
Ts d51Testing软件测试网"M0R }%K)B3Q1S~
public void testBagSumAdd() {'U2A4l.{g!r9y051Testing软件测试网N*h Ak%q"j!C
// {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}"^-aK&Y1s[DY
D&W0Pr0
m
Y9k%BY9Z qT`)z l0IMoney expected= MoneyBag.create(new Money(26, "CHF"), new Money(28, "USD"));jD o(V3BXw0
Cm{-U;J,c.k{0assertEquals(expected, fMB1.add(fMB2));51Testing软件测试网 x A$mr'mR|%a51Testing软件测试网MU G+cD
}51Testing软件测试网B!J6AA'}
{/X[p51Testing软件测试网1gXD!? \,aM8HJi;^
public void testIsZero() {Q j'fF9ml0