{"id":330,"date":"2011-10-11T23:11:34","date_gmt":"2011-10-12T06:11:34","guid":{"rendered":"http:\/\/blogs.unsw.edu.au\/comp1400\/?p=330"},"modified":"2011-10-11T23:16:05","modified_gmt":"2011-10-12T06:16:05","slug":"ass-3-unit-testing","status":"publish","type":"post","link":"https:\/\/blogs.unsw.edu.au\/comp1400\/blog\/2011\/10\/ass-3-unit-testing\/","title":{"rendered":"Ass 3 Unit Testing"},"content":{"rendered":"<p>It turns out that testing the <code>play()<\/code> method on your classes is tricker than I had anticipated when I set assignment 3. To test this method properly, you need to set up a game in a known situation, forcing one of the players to play the card you are testing and then check that it does what you expect.<br \/>\n<!--more--><br \/>\nThe second UnoGame constructor is useful in this situation, as it allows you to stack the deck in a controlled fashion. For example. you could test the DrawCard as follows:<br \/>\n<code><\/p>\n<pre>\r\n    public void testPlay()   {\r\n        \/\/ stack the deck\r\n        ArrayList deck = new ArrayList();\r\n                \r\n        DrawCard redDraw2 = new DrawCard(Card.COLOUR_RED, 2);\r\n        \r\n        \/\/ player 0's cards - one playable and 4 unplayable\r\n        deck.add(redDraw2);\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        \r\n        \/\/ player 1's cards - it doesn't matter what these are\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n        deck.add(new Card(Card.COLOUR_BLUE, 5));\r\n\r\n        \/\/ the card that becomes the initial pile\r\n        \/\/ chosen to allow player 0 to play the red draw 2\r\n        \r\n        deck.add(new Card(Card.COLOUR_RED, 1));\r\n        \r\n        \/\/ the draw pile -- add enough cards for player1 to draw 2\r\n        \r\n        deck.add(new Card(Card.COLOUR_GREEN, 2));\r\n        deck.add(new Card(Card.COLOUR_GREEN, 3));\r\n        deck.add(new Card(Card.COLOUR_GREEN, 4));\r\n\r\n        \/\/ create a game using this deck (unshuffled)\r\n        UnoGame game = new UnoGame(deck, 2);\r\n        game.dealCards();\r\n        \r\n        \/\/ get the two players\r\n        Player player0 = game.getPlayers().get(0);\r\n        Player player1 = game.getPlayers().get(1);\r\n        \r\n        \/\/ player 0 plays a turn\r\n        game.playTurn();\r\n        \r\n        \/\/ check the card played is the red draw 2\r\n        assertEquals(redDraw2, game.getPile().get(0));\r\n        \r\n        \/\/ check how many cards each player has\r\n        assertEquals(4, player0.getCards().size());\r\n        assertEquals(7, player1.getCards().size());\r\n        \r\n        \/\/ check that we skipped player1's turn\r\n        assertEquals(player0, game.getCurrentPlayer());        \r\n        \r\n        \r\n    }\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It turns out that testing the play() method on your classes is tricker than I had anticipated when I set assignment 3. To test this method properly, you need to set up a game in a known situation, forcing one of the players to play the card you are testing and then check that it [&hellip;]<\/p>\n","protected":false},"author":80,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[468],"tags":[],"class_list":["post-330","post","type-post","status-publish","format-standard","hentry","category-assignments"],"_links":{"self":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/users\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/comments?post=330"}],"version-history":[{"count":4,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/330\/revisions"}],"predecessor-version":[{"id":334,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/posts\/330\/revisions\/334"}],"wp:attachment":[{"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/media?parent=330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/categories?post=330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.unsw.edu.au\/comp1400\/wp-json\/wp\/v2\/tags?post=330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}