51Testing软件测试网:f1\G2T-xSh6NzL^Y 今天学习怎么做mock,感觉要用mock必须在开发的时候就要规划好。在《单元测试之道java版》中的一个例子,真实实现的代码是:
"S'U"S2[+^,n%N D0"aVe,^S7t:Y.z8d)^0public class SystemEnvironmental implements Environmental {
Xuh$Jw
R0 public long getTime(){51Testing软件测试网b`&g_(\
w
return System.currentTimeMillis();
L
iRK4k5F"U0 }
?1e9b2ZP)V4i{*l.S"J4X0}51Testing软件测试网
Ub&M.zxlT
51Testing软件测试网8kQ"U[r;K那么在设计的时候,必须先要有一个接口
YzI!B'Ff'yrN0T5Zf'KxF0public interface Environmental {
M*Q_TS^]4V0 public long getTime();51Testing软件测试网&E&Z%bVc
//other methods omitted...
D:f"g+L?E1~E
A4j[ ?0}51Testing软件测试网#_8EEqRnn
laj*AuG]1Yx0 t0CB)x6?/_1nv0然后SystemEnvironmental类和MockSystemEnvironment类同时实现这个接口
{6h+SC&`051Testing软件测试网8Vq!Q$nVQ+J;uspublic class MockSystemEnvironment implements Environmental {
d"qXaTC c.l0 private long current_time;51Testing软件测试网d7X9Y:u.jV
51Testing软件测试网0YhZ
v/]p+O1H
public long getTime() {
9cg)e"T~7p5I0 return current_time;51Testing软件测试网tap-mJ@
BY/L%WH6Wt
}51Testing软件测试网 m*jz0hKF
;fjm6O3mA0 public void setTime(long aTime) {
.x{$z-ui2U0 current_time = aTime;51Testing软件测试网:LoW.d;lz7m;b
}51Testing软件测试网dWr"K-wlu
}
9Y M+W0k^/j,KC
h0oo!{'e4E
B0E0在这个mock中,可以用setTime方法来设置时间,用来控制mock对象。
j+qem1t0m/X051Testing软件测试网/S.kb)zfgDx0Z当有一个新方法调用getTime()方法时,如:
#wX-C?O5x@UBq051Testing软件测试网,R,Z\gz,fpackage example;51Testing软件测试网L(u#z8Zm3R8n
8pOo-p Sx!a/M0import java.util.Calendar;51Testing软件测试网*\J3wO`$E0M
,i-q/F"|AKaW0public class Checker {
4z.yO1bB0 public Checker(Environmental anEnv) {51Testing软件测试网#Tm*aq&cun
env = anEnv;
/|5T1iO?"f`#B.N"g0 }51Testing软件测试网;J[#Q.p#@[
51Testing软件测试网t4I c,kc)l?
]%~!f
A/_2O /**
5Z&q]!iS0 * 10 * After 5 o’clock, remind people to go home by playing a whistle51Testing软件测试网9}iFhK'E/WVr
*/
q L!C6M@0public long reminder() {
)T"L9U H&b;Nz4W&I5X0 Calendar cal = Calendar.getInstance(); 51Testing软件测试网$x#t@R5Y^PH#U
cal.setTimeInMillis(env.getTime()); 51Testing软件测试网@:B;l0mu#p
int hour = cal.get(Calendar.HOUR_OF_DAY);
M$wrf!F'W0 if (hour >= 13) { // 5:00PM
A u%R}VeN*`0 hour = 13;
yi8L.`R5A/dJ_0 } 51Testing软件测试网Mw"`.r'T0O?
return hour;
8Nq;~(y],c^x,{;`0 }
h2p"].ijpj{0-Z)B5u\"h0public long getTime(){51Testing软件测试网L,p*zP%F M
PN?l
return env.getTime();
PY$y5P5_{-NT0}51Testing软件测试网;qtGN&zD B?8Ry
// ...51Testing软件测试网IT6W|PrC E!I7f&d
private Environmental env;
,~ A
L1AL6Y)p0}51Testing软件测试网^,wF%fld
51Testing软件测试网 VyMdT"i#lV 在真实的环境中,传入的参数是真实的SystemEnvironment,则得到的是真实的时间,在测试环境中,传入参数是MockSystemEnvironment,则得到的是设置的时间,可以进行测试。51Testing软件测试网pyJZ0lo8h
51Testing软件测试网S'De~p\3`package example;
p/mRJs7w#r2T+J!G051Testing软件测试网P VA4sg!twimport junit.framework.TestCase;51Testing软件测试网Nt!bf/x^
51Testing软件测试网n{V1U%C6E
|public class CheckerTest extends TestCase {
_jANv"x051Testing软件测试网6Rm
^i/u'Y0yK private Checker checker;
/tzl+z8{R0 private MockSystemEnvironment env;
-II]5u#O7i0
/U,U#\\6H;@]z!dT2@0 public CheckerTest(String aa){51Testing软件测试网
o)B-Hw
K
super(aa);51Testing软件测试网 i~.?3[js!j;g
}
(S(l{3KMF^TE0 51Testing软件测试网[&D8W OsE
protected void setUp() throws Exception {
\)bfy#@&G0 super.setUp();
2S.CC4re%`oBI^0 env = new MockSystemEnvironment();51Testing软件测试网jsu+Y~Q2x
checker = new Checker(env);
!K D'DC'Gp0 }
zedS^3cwn2~051Testing软件测试网1g;B]p)e/Y DW protected void tearDown() throws Exception {
6d"UkXzU0 super.tearDown();
4P)X5_'b"|'R0 }51Testing软件测试网%O&X*n,{7C
?
51Testing软件测试网k"Tn:Cw /*
3@#I.l(DX1W0 * Test method for 'example.Checker.reminder()'
[
|$gD/F_1H V5l0 */
zd#C(Mn*zonc c0 public void testGetTime() {
[afm"pY0 env.setTime(13);
Uj*Y
gp0 assertEquals(checker.getTime(),13);
RMr9C%cd/E0 }
/~*XY9[(Q
KA0 51Testing软件测试网$G@'NS[;I^3j
public void testReminder() {
!t Zpucx1By9k)C*v0 env.setTime(13);
AR
PM)}5Y$|$e)O0 assertEquals(checker.reminder(),13);51Testing软件测试网(C/o'm,x8m0C7SJ
}51Testing软件测试网"T`1p!cu]VC~.G{
}51Testing软件测试网U}!wx].]V"d
us \R6M0