ã¡ã¢: JUnitã®expectedãã£ã¼ã«ãã¨Ruleã¢ããã¼ã·ã§ã³ã§ä¾å¤ã®ãã¹ã
次ã®è¨äºãèªãã§ãåãã¦ç¥ã£ãã®ã§ã¡ã¢ã
- Testing Custom Exceptions w/ JUnit's ExpectedException and @Rule
ãã®ãããªãã¹ã対象ã®ã³ã¼ããããã¨ãã¾ãã
import java.util.ArrayList; import java.util.List; public class Deck { /** * æå®ãã人æ°ã®ãã¬ã¤ã¤ã¼ã«ãå±±æãåä¸ã«é ããåä¸ã«é ããªãå ´åããã¬ã¤ã¤ã¼éã®é å¸ææ°ã®å·®ç°ã¯1æ以å ã¨ããã */ public List<List<Card>> divideCards(int playerNum) { if (playerNum < 2) { throw new IllegalArgumentException("ãã¬ã¤ã¤ã¼æ°ã¯2以ä¸ã§ãªããã°ãªããªãã"); } List<List<Card>> result = new ArrayList<List<Card>>(playerNum); // ç¥ return result; } } class Card { // ç¥ }
divideCardsã¡ã½ããã¯ãå±±æããã¬ã¤ã¤ã¼ã«é ãã¾ããããã§ãã²ã¼ã ä¸ã®ãã¬ã¤ã¤ã¼ãã2人以ä¸ã§ãããã¨ãæ³å®ãã¦ãã¾ãããã1人以ä¸ã®ãã¬ã¤ã¤ã¼ã«å¯¾ãã¦å±±æãé ããã¨ããã¨ãä¾å¤IllegalArgumentExceptionãçºçãã¾ãã
ãã®ã³ã¼ãã®ãã¹ãã¯ãããã¾ã§ãããªæãã§æ¸ãã¦ãã¾ããã
import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import org.junit.Test; public class DeckTest { Deck deck; @Test public void testDivideCardsForException() throws Exception { deck = new Deck(); try { deck.divideCards(0); fail("ããã«ã¯æ¥ãªã"); } catch (IllegalArgumentException expected) { assertEquals("ä¾å¤ã®ã¡ãã»ã¼ã¸ç¢ºèª", expected.getMessage(), "ãã¬ã¤ã¤ã¼æ°ã¯2以ä¸ã§ãªããã°ãªããªãã"); } } }
ããããä»ã¯ãããªãµãã«ç°¡æ½ã«æ¸ããã®ã§ããã
import org.junit.Test; public class DeckTest { Deck deck; @Test(expected = IllegalArgumentException.class) public void testDivideCardsForException() throws Exception { deck = new Deck(); deck.divideCards(0); } }
Testã¢ããã¼ã·ã§ã³ã®expectedãã£ã¼ã«ãã«ãå®è¡æã«çºçããä¾å¤ã¯ã©ã¹ãæå®ããã®ããã¤ã³ãã§ãã
ãããããã®æ¸ãæ¹ã«ã¯æ¬ ç¹ãããããã§ãã
ãã¨ãã°ããã£ãã¨å°ãéã£ã¦ããã¹ã対象ã®ã³ã¼ãããããªæãã ã¨ãã¾ãã
import java.util.ArrayList; import java.util.List; public class Deck { /** * æå®ãã人æ°ã®ãã¬ã¤ã¤ã¼ã«ãå±±æãåä¸ã«é ããåä¸ã«é ããªãå ´åããã¬ã¤ã¤ã¼éã®é å¸ææ°ã®å·®ç°ã¯1æ以å ã¨ããã */ public List<List<Card>> divideCards(int cardNum, int playerNum) { if (cardNum < 1) { throw new IllegalArgumentException("å±±æã¯1æ以ä¸ãªããã°ãªããªãã"); } if (playerNum < 2) { throw new IllegalArgumentException("ãã¬ã¤ã¤ã¼æ°ã¯2以ä¸ã§ãªããã°ãªããªãã"); } List<List<Card>> result = new ArrayList<List<Card>>(playerNum); // ç¥ return result; } } class Card { // ç¥ }
ä¸ã®ã³ã¼ãã§ã¯ãdivideCardsã¡ã½ããã®å¼æ°ã2ã¤ã«ãªã£ã¦ãã¦ãIllegalArgumentExceptionãæããåå ããã1ã¤ç®ã®å¼æ°ãä¸æ£ãªã¨ããã¨ã2ã¤ç®ã®å¼æ°ãä¸æ£ãªã¨ããã®2ãã¿ã¼ã³ã«å¢ãã¦ãã¾ããï¼ãã¡ããããã¹ãã±ã¼ã¹ã®èãæ¹ã«ãã£ã¦ã¯ãã両æ¹èª¤ã£ã¦ããã¨ãããå ããã¾ãããããã¯ãã¦ããï¼
expectedãã£ã¼ã«ãã使ãæ¸ãæ¹ã§ã¯ããã¹ã対象ã³ã¼ãã«ä¾å¤ãæããåå ãè¤æ°ããã¨ããããããã®åå ãèå¥ã§ãã¾ãããIllegalArgumentExceptionãé£ãã§ãããã¨ã ãããæ¤åºã§ããªãã®ã§ããã
ããããã¨ãã¯ããããªãµãã«æ¸ãã°ããããã§ãã
import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class DeckTest { Deck deck; @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void testDivideCardsForDeckException() throws Exception { deck = new Deck(); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("å±±æã¯1æ以ä¸ãªããã°ãªããªãã"); deck.divideCards(0, 2); } @Test public void testDivideCardsForPlayerException() throws Exception { deck = new Deck(); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("ãã¬ã¤ã¤ã¼æ°ã¯2以ä¸ã§ãªããã°ãªããªãã"); deck.divideCards(2, 0); } }
Ruleã¢ããã¼ã·ã§ã³ãã¤ãããExpectedExceptionåã®ãã£ã¼ã«ãå¤æ°ããã¤ã³ãã§ãããã®å¤æ°ã«ä¾å¤ã®åãã¡ãã»ã¼ã¸ãè¨å®ãã¦ããã¹ããå®è¡ããã¨ãæ¤è¨¼ãè¡ãã¾ãã
ã¡ã¢ã¯ä»¥ä¸ã§ãã
ï¼2012/10/3 追è¨ï¼1ã¤ã®ã¡ã½ããã«ããã¹ã対象ã³ã¼ãå¼ã³åºãã2åæ¸ãã¦ãã¾ããããåãã¾ããã1åç®ã®å¼ã³åºãããå®è¡ãããªããããééã£ã¦ãã¾ãããshuji_w6eãããbackpaper0ããããææãããã¨ããããã¾ãã
- ãã®è¨äºã®ç¶ã: メモ: テスト対象メソッドの実行後、テストメソッドは即座に終了される - 虎塚