![]() |
|
[Java] Need Help with Java and Junit - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Java] Need Help with Java and Junit (/showthread.php?tid=5151075) |
Need Help with Java and Junit - legenda008 - Jan 29, 2016 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 and I have writen the code for it Here is my Bowling Class Code public class BowlingGame {and here is my Test Class Code public class TestBowling {I need to do this or 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 * - csrk - Jan 29, 2016 When using JUnit to test your code you want to create a new instance of BowlingGame in every test. Code @TestUnit testing means that for your input you state explicitly what you expect the state to be like. I am not going to do your homework for you but there are alot of other methods. I.e assertArrayEquals(array 1, array 2); assertFalse(bool);. Also, remember to write alot of tests. You never do more than 1 assert in a single test. If the first assert fails and you have another one after that, the second assert will never be evaluated. You might think the 2nd one is the one that fails and spend time debugging a method that works. What you do is write two tests, use the exact same input and just assert for different things. * - legenda008 - Jan 29, 2016 Ok i figured it out ,,but I have a new problem how do i make that if two values in frame cannot be bigger then 10 .or that the sum of throws in one frame cannot be bigger than 10 I hava made this code for(int frame = 0;frame<10;frame++){ if(rolls[cursor] + rolls[cursor+1]>10){ throw new Illegal Argument Exception } 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; } but when I test it with Junit it is not working @Test (expected = IllegalArgumentException.class) public void testFrames(){ game.roll(8,8); } * - csrk - Jan 30, 2016 What do you mean is the problem, is the test not succeeding? From what i understand about your code, if any of the inputs in rolls is above 10 (11 or higher) it will throw an exception. game.roll(8,8) should not throw an exception? I've not gone through your code entirely. |