璇磋鍒濈敤 Mock 宸ュ叿娴嬭瘯纰板埌鐨勫潙
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了璇磋鍒濈敤 Mock 宸ュ叿娴嬭瘯纰板埌鐨勫潙相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/ted' title='ted'>ted
return 鏈€绠€ 鐩爣 googl public 娴佽 .net 瀛︽牎鎴戞槸涓€涓湪鏍″疄涔犵敓锛屼綔涓轰竴涓▼搴忕尶锛屾槸涓彍楦熶腑鎴樻枟鏈猴紒瀵逛簬娴嬭瘯锛屽彧鍐欒繃涓€鐐圭偣绠€鍗曞埌涓嶈兘鍐嶇畝鍗曚簡鐨?Junit 鍗曞厓娴嬭瘯鐨勪緥瀛愶紙鍥犱负褰撴椂杩欒冻浠ュ簲浠樺鏍¤绋嬬殑鍐呭涓庤姹傦級銆傝繖鍑犲ぉ鍦ㄥ叕鍙搁噷瑕佺湡鏋疄寮瑰仛娴嬭瘯鐨勬椂鍊欙紝灏辨繁娣变綋浼氬埌浜嗏€滀功鍒扮敤鏃舵柟鎭ㄥ皯鈥濊繖鍙ヨ瘽鐨勭湡璋涗簡銆傛病鍔炴硶锛岃皝鍙垜褰撳垵涓嶅娣卞叆鐨勫鐐瑰憿銆備簬鏄紝鍦ㄦ潨鍙旀潨澶х鐨勬寚瀵间笅锛屽紑濮嬩簡鑿滈笩鐨勫垵鐢?Mock 宸ュ叿锛岃竟瀛﹁竟鐢ㄧ殑鐖潙涔嬫梾銆?/p>
涓轰粈涔堣鐢?Mock 宸ュ叿锛?/h3>
璇濅笉澶氳锛屾垜浠厛鐩存帴鐪嬬湅涓渶绠€鍗曠殑渚嬪瓙锛?br>娴嬭瘯鐩爣锛?/p>
public class ToBeTested {
public int add(int a, int b) {
return a+b;
}
public int plus(int a, int b){
return a*b;
}
}
娴嬭瘯鐢ㄤ緥锛?/p>
public class test {
@Test
public void testEquals(){
ToBeTested t = new ToBeTested();
assertEquals(3,t.add(1,2));
assertEquals(8,t.plus(2,4));
}
}
杩欓噷鎴戜滑鏄洿鎺ラ€氳繃 new 鏉ユ瀯寤轰簡涓€涓?ToBeTested 鐨勫疄渚嬶紝鍥犱负杩欎釜绫荤畝鍗曪紝鑰屼笖寰呮祴璇曠殑鏂规硶閲屼篃娌℃湁渚濊禆浠讳綍澶栭儴鐨勫璞★紝灏变竴涓畝鍗曠殑鍔犳硶鎴栦箻娉曞氨瀹屼簨浜嗐€備絾浜嬫儏鎬绘槸娌℃湁閭d箞绠€鍗曪紝鍦ㄥ仛鍗曞厓娴嬭瘯鐨勬椂鍊欙紝鎴戜滑瑕佹祴璇曠殑鏂规硶寰€寰€閮芥槸闇€瑕佷緷璧栧緢澶氬閮ㄧ殑瀵硅薄锛屾瘮濡傜綉缁滈€氳锛岃繙绋嬫湇鍔′箣绫荤殑锛岃繖浜涘閮ㄥ璞℃槸鎴戜箞娌℃硶鎺у埗鐨勩€傞毦閬撹繖浜涘閮ㄤ緷璧栫殑瀵硅薄閮介渶瑕?new 涓€涓嚭鏉ュ悧 NO! 鎴戜滑鏈?Mock! 鎴戜滑鍙互鐢?Mock 宸ュ叿鏉ユā鎷熻繖浜涘閮ㄥ璞★紝鏉ュ畬鎴愭垜浠殑鍗曞厓娴嬭瘯銆傝繕鏄厛鏉ョ湅涓€涓畝鍗曠殑 Mock 娴嬭瘯鐨勪緥瀛愬惂锛?/p>
public class test {
@Test
public void test(){
List<String> list = Mockito.mock(List.class);
list.add("coding");
verify(list).add("coding");
}
}
鍧? 锛?spy
濡傛灉瑕佺敤鐪熷疄瀵硅薄锛堣€屼笉鏄?Mock 鍑烘潵鐨勮櫄鎷熷璞★級涓殑鐪熷疄鐨勬柟娉曪紝鍒欓渶瑕?spy 涓€涓嬶紒鐪嬩笅闈㈢殑渚嬪瓙锛?/p>
鎴戝湪娴嬭瘯鐩爣浠g爜涓姞浜嗕竴涓?compute 鍑芥暟锛?/p>
public int compute(int a){
int b = add(a,a);
int c = plus(a,a);
return b+c;
}
闅忓悗鎯冲綋鐒剁殑鍐欎簡涓搴旂殑娴嬭瘯鐢ㄤ緥锛?/p>
@Test
public void testCompute(){
ToBeTested t = new ToBeTested();
assertEquals(8, t.compute(2));
}
浣嗚仾鏄庣殑浣犱竴鐪嬪氨鐭ラ亾锛岃繖涓嶅彨鍗曞厓娴嬭瘯銆傚洜涓烘垜鍙槸鎯虫祴璇?compute 杩欎釜鍑芥暟鏄笉鏄纭殑锛屼絾 compute 鍗翠緷璧栦簬 add 鍜?plus 杩欎袱涓嚱鏁般€傚鏋?add 鍑芥暟閲屾湁閿欙紝涔熶細瀵艰嚧 compute 鍑洪敊锛屽彲杩欐牴鏈笉鍏?compute 鐨勪簨鍟娿€傛墍浠ワ紝娴嬭瘯鐢ㄤ緥浠g爜搴旇鏄繖鏍风殑锛?/p>
@Test
public void testCompute(){
ToBeTested t = spy(new ToBeTested());
when(t.add(2,2)).thenReturn(4);
when(t.plus(2, 2)).thenReturn(4);
assertEquals(8, t.compute(2));
}
娉ㄦ剰锛佽繖閲?new 涓€涓湡瀹炲璞?t 鐨勬椂鍊欙紝闇€瑕?spy 涓€涓嬶紒(褰撴椂骞磋交涓嶆噦浜嬶紝琚繖閲屽崱浜嗙暐涔呫€傘€?鍥犱负杩欓噷鐢ㄥ埌浜嗕緷璧栫殑鏂规硶 add 鍜?plus銆傜幇鍦ㄥ浜庤繖涓緥瀛愶紝compute 鐨勭粨鏋滃凡缁忎笉渚濊禆鍒汉浜嗭紝鍝€?add 鏂规硶閲岋紝涓嶇煡鏄摢涓矖蹇冪殑绋嬪簭鐚垮啓鎴愪簡 鈥渞eturn a - b ",杩欎釜娴嬭瘯涔熸槸閫氳繃鐨勶紝鍥犱负杩欓噷鏈?nbsp;when(t.add(2,2)).thenReturn(4);
杩欏彞璇濓紝stubbing 浜?add 杩欎釜鏂规硶銆傝繖涔熺鍚堝崟鍏冩祴璇曠殑姒傚康锛屾垜浠幇鍦ㄥ彧璐熻矗娴嬭瘯 compute 杩欎釜鍑芥暟锛屾墠涓嶇 add 鎴?plus 姝g‘涓庡惁鍛€?/p>
鍧?锛歞oReturn
绛夌瓑锛屽鍒氬垰鎵€璇达紝compute 宸茬粡涓嶄緷璧?add 鐨勮繑鍥炵粨鏋滀簡锛岄偅鏄笉鏄綋鎴戜滑娴嬭瘯 compute 鍑芥暟鐨勬椂鍊欙紝add 鍑芥暟鏄笉鏄氨鍙互瀹屽叏鏃犳硶鏃犲ぉ鐨勪贡鏉ヤ簡鍛紵鎴戜滑鏉ョ湅鐪嬭繖涓細
public int add(int a, int b) {
ArrayList<String> list = new ArrayList<String>();
String s = list.get(0);
return a+b;
}
杩欐椂锛屽綋鎴戜滑鍐嶆鎵ц娴嬭瘯鏃讹紝灏辨姏寮傚父浜嗭細java.lang.IndexOutOfBoundsException
銆傛樉鐒?nbsp;list.get(0)
鏄姜榄佺ジ棣栵紒濂囨€紵涓嶆槸璇?compute 宸茬粡涓嶇 add 浜嗗悧锛?add 閲岄潰鐨勪唬鐮佹湁闂鍙堟€垜鍜紵鍒繕浜嗭紝杩欒繕鏄?java 璇█锛佹墍浠ワ紝when(t.add(2,2)).thenReturn(4);
杩欒鍙ヨ繕鏄細鍏堝幓鎵ц涓€閬?when 閲岄潰鐨勫嚱鏁帮紝 add(2,2). 鑷充簬杩斿洖缁撴灉鏄彟澶栦竴鍥炰簨銆傞偅涔堟€庝箞瑙e喅杩欎釜闂鍛紵瀵癸紒鐢?doReturn 璇彞!
@Test
public void testCompute(){
ToBeTested t = spy(new ToBeTested());
//when(t.add(2,2)).thenReturn(4);
doReturn(4).when(t).add(2,2);
when(t.plus(2, 2)).thenReturn(4); ;
assertEquals(8, t.compute(2));
}
鐜板湪濂戒簡锛?compute 宸茬粡瀹屽叏涓嶅彈 add 鍑芥暟鐨勫奖鍝嶄簡銆俛dd 鍑芥暟鐖辨€庝箞鐤氨鎬庝箞鐤紝涓嶄細褰卞搷 compute 鍑芥暟鐨勬祴璇曠粨鏋滀簡锛岃嚦姝わ紝 compute 缁堜簬鍙互瀹夐潤鍦板畬鎴愬畠鐨勫崟鍏冩祴璇曚簡銆?/p>
鍧?锛?PowerMock
鐜板浠婃瘮杈冩祦琛岀殑 Mock 宸ュ叿濡?jMock銆丒asyMock銆丮ockito 绛夐兘鏈変竴涓叡鍚岀殑缂虹偣锛氫笉鑳?mock 闈欐€併€乫inal銆佺鏈夋柟娉曠瓑銆傝€?PowerMock 鑳藉瀹岀編鐨勫讥琛ヤ互涓婁笁涓?Mock 宸ュ叿鐨勪笉瓒炽€傝嚦浜庢€庝箞鍋氾紝鍘?Google 涓€涓嬩竴澶ф妸鐨勬暀绋嬩笌瀹炰緥锛岃繖閲屽氨涓嶅啀澶氳浜嗐€傘€?/p>
鍧?锛?锛?锛?锛屻€傘€傘€傘€傘€?/h3>
鎱㈡參韪?:)
from: https://blog.coding.net/blog/mock-testing-tools
以上是关于璇磋鍒濈敤 Mock 宸ュ叿娴嬭瘯纰板埌鐨勫潙的主要内容,如果未能解决你的问题,请参考以下文章