基于测试的JAVA开发案例.ppt

上传人:牧羊曲112 文档编号:5951913 上传时间:2023-09-07 格式:PPT 页数:53 大小:452KB
返回 下载 相关 举报
基于测试的JAVA开发案例.ppt_第1页
第1页 / 共53页
基于测试的JAVA开发案例.ppt_第2页
第2页 / 共53页
基于测试的JAVA开发案例.ppt_第3页
第3页 / 共53页
基于测试的JAVA开发案例.ppt_第4页
第4页 / 共53页
基于测试的JAVA开发案例.ppt_第5页
第5页 / 共53页
点击查看更多>>
资源描述

《基于测试的JAVA开发案例.ppt》由会员分享,可在线阅读,更多相关《基于测试的JAVA开发案例.ppt(53页珍藏版)》请在三一办公上搜索。

1、,Bowling Game Kata,Object Mentor,Inc.,fitnesse.org,Copyright 2005 by Object Mentor,IncAll copies must retain this page unchanged.,Scoring Bowling.,The game consists of 10 frames as shown above.In each frame the player hastwo opportunities to knock down 10 pins.The score for the frame is the totalnum

2、ber of pins knocked down,plus bonuses for strikes and spares.A spare is when the player knocks down all 10 pins in two tries.The bonus forthat frame is the number of pins knocked down by the next roll.So in frame 3above,the score is 10(the total number knocked down)plus a bonus of 5(thenumber of pin

3、s knocked down on the next roll.)A strike is when the player knocks down all 10 pins on his first try.The bonusfor that frame is the value of the next two balls rolled.In the tenth frame a player who rolls a spare or strike is allowed to roll the extraballs to complete the frame.However no more than

4、 three balls can be rolled intenth frame.,The Requirements.,Write a class named“Game”that has two methodsroll(pins:int)is called each time the player rolls a ball.The argument is the number of pins knocked down.score():int is called only at the very end of the game.It returns the total score for tha

5、t game.,A quick design session,Clearly we need the Game class.,A quick design session,A game has 10 frames.,A quick design session,A frame has 1 or two rolls.,A quick design session,The tenth frame has two or three rolls.It is different from all the other frames.,A quick design session,The score fun

6、ction mustiterate through all theframes,and calculateall their scores.,A quick design session,The score for a spare or a strike depends on the frames successor,Begin.,Create a project named BowlingGameCreate a unit test named BowlingGameTest,import junit.framework.TestCase;public class BowlingGameTe

7、st extends TestCase,Begin.,Create a project named BowlingGameCreate a unit test named BowlingGameTest,import junit.framework.TestCase;public class BowlingGameTest extends TestCase,Execute this program and verify that you get the following error:No tests found in BowlingGameTest,The first test.,impor

8、t junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();,The first test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();,publi

9、c class Game,The first test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();,public class Game,The first test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testG

10、utterGame()throws Exception Game g=new Game();for(int i=0;i20;i+)g.roll(0);,public class Game,The first test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();for(int i=0;i20;i+)g.roll(0);,public class Game p

11、ublic void roll(int pins),The first test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();for(int i=0;i20;i+)g.roll(0);assertEquals(0,g.score();,public class Game public void roll(int pins),The first test.,i

12、mport junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();for(int i=0;i20;i+)g.roll(0);assertEquals(0,g.score();,public class Game public void roll(int pins)public int score()return-1;,expected:but was:,The first test.

13、,import junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();for(int i=0;i20;i+)g.roll(0);assertEquals(0,g.score();,public class Game public void roll(int pins)public int score()return 0;,The Second test.,import junit.f

14、ramework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();for(int i=0;i 20;i+)g.roll(0);assertEquals(0,g.score();public void testAllOnes()throws Exception Game g=new Game();for(int i=0;i 20;i+)g.roll(1);assertEquals(20,g.score();,p

15、ublic class Game public void roll(int pins)public int score()return 0;,The Second test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase public void testGutterGame()throws Exception Game g=new Game();for(int i=0;i 20;i+)g.roll(0);assertEquals(0,g.score();public void test

16、AllOnes()throws Exception Game g=new Game();for(int i=0;i 20;i+)g.roll(1);assertEquals(20,g.score();,public class Game public void roll(int pins)public int score()return 0;,-Game creation is duplicated-roll loop is duplicated,The Second test.,import junit.framework.TestCase;public class BowlingGameT

17、est extends TestCase public void testGutterGame()throws Exception Game g=new Game();for(int i=0;i 20;i+)g.roll(0);assertEquals(0,g.score();public void testAllOnes()throws Exception Game g=new Game();for(int i=0;i 20;i+)g.roll(1);assertEquals(20,g.score();,public class Game public void roll(int pins)

18、public int score()return 0;,-Game creation is duplicated-roll loop is duplicated,expected:but was:,The Second test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();public void testGutterGame()throws Exce

19、ption for(int i=0;i 20;i+)g.roll(0);assertEquals(0,g.score();public void testAllOnes()throws Exception for(int i=0;i 20;i+)g.roll(1);assertEquals(20,g.score();,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-roll loop is duplicated,The Sec

20、ond test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();public void testGutterGame()throws Exception int n=20;int pins=0;for(int i=0;i n;i+)g.roll(pins);assertEquals(0,g.score();public void testAllOnes

21、()throws Exception for(int i=0;i 20;i+)g.roll(1);assertEquals(20,g.score();,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-roll loop is duplicated,The Second test.,import junit.framework.TestCase;public class BowlingGameTest extends TestC

22、ase private Game g;protected void setUp()throws Exception g=new Game();public void testGutterGame()throws Exception int n=20;int pins=0;rollMany(n,pins);assertEquals(0,g.score();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testAllOnes()throws Exception for(int i=0

23、;i 20;i+)g.roll(1);assertEquals(20,g.score();,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-roll loop is duplicated,The Second test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected v

24、oid setUp()throws Exception g=new Game();public void testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testAllOnes()throws Exception for(int i=0;i 20;i+)g.roll(1);assertEquals(20,g.score();,public

25、 class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-roll loop is duplicated,The Second test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();public void

26、 testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();,public class Game private int score=0;public void roll(int pins)score+=pi

27、ns;public int score()return score;,-roll loop is duplicated,The Second test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public vo

28、id testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,The Third test.,import junit.fra

29、mework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testA

30、llOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();public void testOneSpare()throws Exception g.roll(5);g.roll(5);/spare g.roll(3);rollMany(17,0);assertEquals(16,g.score();,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-ugl

31、y comment in test.,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMa

32、ny(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();public void testOneSpare()throws Exception g.roll(5);g.roll(5);/spare g.roll(3);rollMany(17,0);assertEquals(16,g.score();,public class Game private int score=0;public void roll(int pi

33、ns)score+=pins;public int score()return score;,-ugly comment in test.,expected:but was:,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i

34、n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();public void testOneSpare()throws Exception g.roll(5);g.roll(5);/spare g.roll(3);rollMany(17,0);assertEquals(16,g.s

35、core();,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-ugly comment in test.,tempted to use flag to remember previous roll.So design must be wrong.,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestC

36、ase private Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.

37、score();public void testOneSpare()throws Exception g.roll(5);g.roll(5);/spare g.roll(3);rollMany(17,0);assertEquals(16,g.score();,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-ugly comment in test.,roll()calculates score,but name does no

38、t imply that.,score()does not calculate score,but name implies that it does.,Design is wrong.Responsibilities are misplaced.,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();private void r

39、ollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();/public void testOneSpare()throws Exception/g.roll(5);/g.roll(5);/spare/g.rol

40、l(3);/rollMany(17,0);/assertEquals(16,g.score();/,public class Game private int score=0;public void roll(int pins)score+=pins;public int score()return score;,-ugly comment in test.,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected

41、void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();/public void testOneS

42、pare()throws Exception/g.roll(5);/g.roll(5);/spare/g.roll(3);/rollMany(17,0);/assertEquals(16,g.score();/,public class Game private int score=0;private int rolls=new int21;private int currentRoll=0;public void roll(int pins)score+=pins;rollscurrentRoll+=pins;public int score()return score;,-ugly com

43、ment in test.,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMany(20

44、,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();/public void testOneSpare()throws Exception/g.roll(5);/g.roll(5);/spare/g.roll(3);/rollMany(17,0);/assertEquals(16,g.score();/,public class Game private int score=0;private int rolls=new i

45、nt21;private int currentRoll=0;public void roll(int pins)score+=pins;rollscurrentRoll+=pins;public int score()int score=0;for(int i=0;i rolls.length;i+)score+=rollsi;return score;,-ugly comment in test.,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase pri

46、vate Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public void testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score()

47、;/public void testOneSpare()throws Exception/g.roll(5);/g.roll(5);/spare/g.roll(3);/rollMany(17,0);/assertEquals(16,g.score();/,public class Game private int rolls=new int21;private int currentRoll=0;public void roll(int pins)rollscurrentRoll+=pins;public int score()int score=0;for(int i=0;i rolls.l

48、ength;i+)score+=rollsi;return score;,-ugly comment in test.,The Third test.,import junit.framework.TestCase;public class BowlingGameTest extends TestCase private Game g;protected void setUp()throws Exception g=new Game();private void rollMany(int n,int pins)for(int i=0;i n;i+)g.roll(pins);public voi

49、d testGutterGame()throws Exception rollMany(20,0);assertEquals(0,g.score();public void testAllOnes()throws Exception rollMany(20,1);assertEquals(20,g.score();public void testOneSpare()throws Exception g.roll(5);g.roll(5);/spare g.roll(3);rollMany(17,0);assertEquals(16,g.score();,public class Game pr

50、ivate int rolls=new int21;private int currentRoll=0;public void roll(int pins)rollscurrentRoll+=pins;public int score()int score=0;for(int i=0;i rolls.length;i+)score+=rollsi;return score;,-ugly comment in test.,expected:but was:,The Third test.,import junit.framework.TestCase;public class BowlingGa

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号