vendredi 29 janvier 2016

java testing junit help wrong input

Ok guys I neeed quick help with my assigment I think it is not something difficult

You see I have to write Bowling Code kata like this

http://ift.tt/1nCkBuI

and I have writen the code for it

Here is my Bowling Class

public class BowlingGame {



    private int roll = 0;
    private int [] rolls = new int[21];


    public void roll(int...rolls){
        for(int pinsDown:rolls){
            if(pinsDown>10){
                throw new IllegalArgumentException();
            }else{
            roll(pinsDown);
            }

        }

    }


    public void roll(int pinsDown){
        rolls[roll++] =pinsDown;
    }


    public int score(){
        int score = 0;
        int cursor = 0;

        for(int frame = 0;frame<10;frame++){
            if(rolls[cursor]==10){ // check if it is strike
                score+=10 + rolls[cursor+1] + rolls[cursor+2];
                cursor++;
            }else if(rolls[cursor] + rolls[cursor+1]==10){ // check if it is spare
                score+=10 + rolls[cursor+2];
                cursor+=2;
            }else{
                score+=rolls[cursor] + rolls[cursor+1];
                cursor+=2;
            }
        }
        return score;

    }

}

and Here is my Testing Class

public class TestBowling {



    private BowlingGame game;

    @Before
    public void setingUp(){
        game = new BowlingGame();
    }

    @Test
    public void canScore60(){
        game.roll(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3);
        assertThat(game.score(), is(60));

    }



    @Test
    public void canScorePerfect(){
        game.roll(10,10,10,10,10,10,10,10,10,10,10,10);
        assertThat(game.score(), is(300));

    }


    @Test
    public void canScore67(){
        game.roll(3,3,3,3,3,3,3,3,4,6,3,3,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(67));

    }


    @Test
    public void canScore75(){
        game.roll(3,3,3,3,3,3,3,3,4,6,4,6,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(75));

    }


    @Test
    public void canScore70(){
        game.roll(3,3,3,3,3,3,3,3,10,3,3,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(70));

    }


    @Test
    public void canScore87(){
        game.roll(3,3,3,3,3,3,3,3,10,10,3,3,3,3,3, 3,3, 3,3);
        assertThat(game.score(), is(87));

    }


    @Test
    public void canScore70with10(){
        game.roll(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,10,3,3);
        assertThat(game.score(), is(70));

    }




    @Test
    public void canScore84(){
        game.roll(3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,10,10,10);
        assertThat(game.score(), is(84));

    }


    @Test
    public void canScoreWith3and7(){
        game.roll(3,7,3,7,3,7,3,7,3,7,3,7,3,7,3,7,3,7,3,7,3);
        assertThat(game.score(), is(130));

    }









    //this tests should fail

    @Test (expected = IllegalArgumentException.class)
    public void testing15() {
        int a = 15;
        game.roll(a);

    }



    @Test (expected = IllegalArgumentException.class)
    public void testingLetter() {
        char c = 'c';
        game.roll(c);

    }


}

**My question is how can I test this

Test wrong input with 15 for one throw

with two throws in the same frame bigger than 10

with a letter input

**

Aucun commentaire:

Enregistrer un commentaire