Studyon Minte9.com
JUnit




Bowling Scores



What we'd like to do is write an application that keeps track of a bowling league. It needs to record all the 
games, determine the ranks of the teams, determine the winners and losers of each weekly match, and 
accurately score each game.

	%java
		package testexample;

		import org.junit.Test;
		import org.junit.Assert.*;
		import junit.framework.*;

		public class Testexample {
		    public static void main(String[] args) {
			new JUnittest().testScore();
		    }
		}

		class Game {

		    private int iScore = 0; // general score
		    private int[] iThrows = new int[21]; // maximum 21 throws in a game
		    private int iCurrentThrow; // current throw
		    
		    public int score() {
			return this.iScore;
		    }
		    
		    public int scoreForFrame(int f) {
			return 0;
		    }
		    
		    public void add(int iPins) {
			iThrows[iCurrentThrow++] = iPins;
			iScore += iPins;
		    }
		}

		class JUnittest extends TestCase {
		    public void testScore() {
			Game g = new Game();
			g.add(4);
			g.add(5);
			g.add(8);

			/* --- LOOK HERE ---*/
			
			assertEquals(17, g.score()); // pass
			
			assertEquals(9, g.scoreForFrame(1)); // fail
			assertEquals(17, g.scoreForFrame(2));
		    }
		}

General score pass, but score for frame don't. Let's correct it.

	%java
		package testexample;

		import org.junit.Test;
		import org.junit.Assert.*;
		import junit.framework.*;

		public class Testexample {
		    public static void main(String[] args) {
			new JUnittest().testScore();
		    }
		}

		class Game {

		    private int iScore = 0; // general score
		    private int[] iThrows = new int[21]; // maximum 21 throws in a game
		    private int iCurrentThrow; // current throw
		    
		    public int score() {
			return this.iScore;
		    }
		    
		    /* --- LOOK HERE --- */
		    
		    public int scoreForFrame(int iFrame) {
			int ball = 0;
			int score = 0;
			for (int iCurrFrame = 0; iCurrFrame < iFrame; iCurrFrame++) {
			    int iFirstThrow = iThrows[ball++];
			    int iSecondThrow = iThrows[ball++];
			    score += iFirstThrow + iSecondThrow;
			}
			return score;
		    }
		    
		    public void add(int iPins) {
			iThrows[iCurrentThrow++] = iPins;
			iScore += iPins;
		    }
		}

		class JUnittest extends TestCase {
		    public void testScore() {
			Game g = new Game();
			g.add(4);
			g.add(5);
			g.add(8);
			
			assertEquals(17, g.score());
			
			/* --- LOOK HERE ---*/
			
			assertEquals(9, g.scoreForFrame(1)); // now it pass
			assertEquals(17, g.scoreForFrame(2));
		    }
		}


Some refactoring with setUp method

	%java

		package testexample;

		import org.junit.Test;
		import org.junit.Assert.*;
		import junit.framework.*;

		public class Testexample {
		    public static void main(String[] args) {
			new JUnittest("Bowling").test();
		    }
		}

		class Game {

		    private int iScore = 0; // general score
		    private int[] iThrows = new int[21]; // maximum 21 throws in a game
		    private int iCurrentThrow; // current throw
		    
		    public int score() {
			return this.iScore;
		    }
			
		    public int scoreForFrame(int iFrame) {
			int ball = 0;
			int score = 0;
			for (int iCurrFrame = 0; iCurrFrame < iFrame; iCurrFrame++) {
			    int iFirstThrow = iThrows[ball++];
			    int iSecondThrow = iThrows[ball++];
			    score += iFirstThrow + iSecondThrow;
			}
			return score;
		    }
		    
		    public void add(int iPins) {
			iThrows[iCurrentThrow++] = iPins;
			iScore += iPins;
		    }
		    
		    public void reset() {
			iScore = 0;
			iThrows = new int[21];
		    }
		}

		class JUnittest extends TestCase {
		    
		    private Game g;
		    
		    public JUnittest(String name) {
			super(name);
			setUp();
		    }

		    /* --- LOOK HERE --- */
		    
		    @Override
		    public void setUp() {
			g = new Game();
		    }

		    /* --- LOOK HERE --- */
		    
		    public void test() {
			testOneThrow(); 
			testTwoThrowsNoMark();
			testFourThrowsNoMark();
		    }
		    
		    public void testOneThrow() {
			g.add(5);
			assertEquals(5, g.score());
			g.reset();
		    }
		    
		    public void testTwoThrowsNoMark() {
			g.add(5);
			g.add(4);
			assertEquals(9, g.score());
			g.reset();
		    }
		    
		    public void testFourThrowsNoMark() {
			g.add(5);
			g.add(4);
			g.add(7);
			g.add(2);
			assertEquals(18, g.score());
			g.reset();
		    }
		}


http://www.objectmentor.com/resources/articles/xpepisode.htm