æ¬è¨äºã§ã¯JUnit5ã«ããããã©ã¡ã¼ã¿åãã¹ãã®ä½¿ãã©ããã¨å®éã®å®è£ æ¹æ³ã«ã¤ãã¦ç´¹ä»ãã¾ãã
使ãã©ãã
ãã¹ãã±ã¼ã¹ãä½æããæã¯è¤æ°ã®æ¯ãèãããã¹ããããã¨ãã»ã¨ãã©ãã¨æãã¾ãã
ä¾ãã°ã以ä¸ã®ããã«åãåã£ãå¹´é½¢ã®å¤ããå¦å¹´ãè¿ãã¡ã½ãããããã¨ãã¾ãã
public String getGrade(int age) { if (age < 0) { return "åå¨ããªãå¹´é½¢"; } if (age <= 5) { return "åå " } else if (age <= 12) { return "å°å¦ç" } else if (age <= 15) { return "ä¸å¦ç" } else if (age <= 18) { return "é«æ ¡ç" } return "大人" }
ãã®å ´åãã¹ããããæ¯ãèãã¯6ã±ã¼ã¹ã§ãã
- åå¨ããªãå¹´é½¢
- åå
- å°å¦ç
- ä¸å¦ç
- é«æ ¡ç
- 大人
å¢çå¤ã§ãã¹ãããããªããããã以ä¸ã®å¤ãå ¥åå¤ã¨ãã¦ãã¹ããããã§ã
- åå¨ããªãå¹´é½¢: -1
- åå : 0, 5
- å°å¦ç: 6, 12
- ä¸å¦ç: 13, 15
- é«æ ¡ç: 16, 18
- 大人: 19
ãã®ãã¹ããç´ ç´ã«æ¸ãã¨ä»¥ä¸ã®ããã«ãªãã¾ãã
public class å¹´é½¢ããå¦å¹´ãå¤å®ããå¦çã®ãã¹ã { @Test public void å¹´é½¢ã0æªæºã®å ´å_åå¨ããªãå¹´é½¢ã¨ãã() { assertEquals("åå¨ããªãå¹´é½¢", getGrade(-1)); } @Test public void å¹´é½¢ã0ã®å ´å_åå ã¨ãã() { assertEquals("åå ", getGrade(0)); } @Test public void å¹´é½¢ã5ã®å ´å_åå ã¨ãã() { assertEquals("åå ", getGrade(5)); } @Test public void å¹´é½¢ã6ã®å ´å_å°å¦çã¨ãã() { assertEquals("å°å¦ç", getGrade(6)); } @Test public void å¹´é½¢ã12ã®å ´å_å°å¦çã¨ãã() { assertEquals("å°å¦ç", getGrade(12)); } @Test public void å¹´é½¢ã13ã®å ´å_ä¸å¦çã¨ãã() { assertEquals("ä¸å¦ç", getGrade(13)); } @Test public void å¹´é½¢ã15ã®å ´å_ä¸å¦çã¨ãã() { assertEquals("ä¸å¦ç", getGrade(15)); } @Test public void å¹´é½¢ã16ã®å ´å_é«æ ¡çã¨ãã() { assertEquals("é«æ ¡ç", getGrade(16)); } @Test public void å¹´é½¢ã18ã®å ´å_é«æ ¡çã¨ãã() { assertEquals("é«æ ¡ç", getGrade(18)); } @Test public void å¹´é½¢ã19以ä¸ã®å ´å_大人ã¨ãã() { assertEquals("大人", getGrade(19)); } }
ãã®ãã¹ãã§ã網ç¾
æ§ã®è¦³ç¹ã§è¨ãã°åé¡ã¯ç¡ãããã§ãã
ãã ãå
¥åå¤ãç°ãªããæå¾
å¤ãåãã¨ããããããåå¤ã¯ã©ã¹ã®ãã¹ããã¡ã½ãããåå²ããã¦ãã¦åé·ã«æãã¾ãã
ã¾ãããããã®ã¡ã½ããåã¯ã·ã¹ãã ã®æ¯ãèããé©åã«è¡¨ãã¦ãã¾ãããä¾ãã°ãä¸è¨ã®ã¡ã½ãããè¦ãã ãã§ã¯å¹´é½¢ã7ã®æã¯ä½ãè¿ã£ã¦ããã®ãåãããªãã®ã§çµå±å®è£
ãè¦ã«è¡ããã¨ã«ãªãã¾ãã
ãã¹ãã¯ã·ã¹ãã ã®ä»æ§ã表ç¾ããã¨ãã大äºãªå½¹å²ãæ
ã£ã¦ãã¾ãããä¸è¨ã®æ¸ãæ¹ã ã¨ä»æ§ãã¡ã½ããåã§è¡¨ç¾ãã¥ãããªã£ã¦ãã¾ãã¾ãã
ãããªå ´é¢ã§ä½¿ããã®ããã©ã¡ã¼ã¿åãã¹ãã§ãã
åå¤ã¯ã©ã¹ã®ç®æããã©ã¡ã¼ã¿åãã¹ãã«ç½®ãæããã®ã以ä¸ã«ãªãã¾ãã
public class å¹´é½¢ããå¦å¹´ãå¤å®ããå¦çã®ãã¹ã { @Test public void å¹´é½¢ã0æªæºã®å ´å_åå¨ããªãå¹´é½¢ã¨ãã() { assertEquals("åå¨ããªãå¹´é½¢", getGrade(-1)); } @ParameterizedTest @ValueSource(ints = {0, 5}) public void å¹´é½¢ã0以ä¸5以ä¸ã®å ´å_åå ã¨ãã(int age) { assertEquals("åå ", getGrade(age)); } @ParameterizedTest @ValueSource(ints = {6, 12}) public void å¹´é½¢ã6以ä¸12以ä¸ã®å ´å_å°å¦çã¨ãã(int age) { assertEquals("å°å¦ç", getGrade(age)); } @ParameterizedTest @ValueSource(ints = {13, 15}) public void å¹´é½¢ã13以ä¸15以ä¸ã®å ´å_ä¸å¦çã¨ãã(int age) { assertEquals("ä¸å¦ç", getGrade(age)); } @ParameterizedTest @ValueSource(ints = {16, 18}) public void å¹´é½¢ã16以ä¸18以ä¸ã®å ´å_é«æ ¡çã¨ãã(int age) { assertEquals("é«æ ¡ç", getGrade(age)); } @Test public void å¹´é½¢ã19以ä¸ã®å ´å_大人ã¨ãã() { assertEquals("大人", getGrade(19)); } }
åå¤ã¯ã©ã¹ããã£ãããã¦è¦ããããªãã¾ããã
ãã¹ãã¡ã½ããåãæ¯ãèãã表ç¾ãããããªãã¾ããããã¹ããè¦ãã ãã§getGradeã¡ã½ãã
ãã©ã®ãããªæ¯ãèããããããåããããã«ãªã£ããã¨æãã¾ãã
ãã©ã¡ã¼ã¿åãã¹ãã便å©ãªãã¨ãåããã¾ããã
ãããã使ãæ¹ã誤ãã¨éã«ãã¹ããåããã«ãããªããã¨ãããã¾ãã
ãã©ã¡ã¼ã¿åãã¹ãã¯è¤æ°ãã¼ã¿ãä¸åº¦ã«ãã©ã¡ã¼ã¿ã¨ãã¦æ¸¡ããã¨ãã§ãã¾ããããããã¨ãä¸è¨ã®ãã¹ãã³ã¼ãããã£ã¨æ¹è¯ãããã¨å
¨ã¦ã®ã±ã¼ã¹ããã©ã¡ã¼ã¿ã«éç´ããããªã£ã¦ãã¾ãã
å®éã«ãã£ã¦ã¿ãã®ã以ä¸ã§ãã
public class å¹´é½¢ããå¦å¹´ãå¤å®ããå¦çã®ãã¹ã { @ParameterizedTest @CsvSource({"-1, åå¨ããªãå¹´é½¢", "0, åå " "5, åå " "6, å°å¦ç" "12, å°å¦ç" "13, ä¸å¦ç" "15, ä¸å¦ç" "16, é«æ ¡ç" "18, é«æ ¡ç" "19, 大人" }) public void ä¸ããããå¹´é½¢ããå¦å¹´ãå¤å®ãã(int age, String grade) { assertEquals(grade, getGrade(age)); } }
ãã¹ãã³ã¼ãã¯çããªãã¾ãããããããä½ããã¹ããã¦ããã®ãããåãããªããªãã¾ããã
åç´ã«ãã©ã¡ã¼ã¿ãå¤ãããã®ãä¸ã¤ã®åå ã§ãããã©ã¡ã¼ã¿ã¯å¤ã§ãããªãã®ã§ããã ããæ¸ããã¦ããã¹ãã®æå³ã¯åããã¾ããã
ã¾ããã¹ãã¡ã½ãããæ±ç¨çãªã¡ã½ããåã«ãªã£ã¦ããã®ãåãããã¨æãã¾ããè²ããªæ¯ãèããä¸åº¦ã«ãã¹ããããã¦å
·ä½çãªå½åãã§ããªããªã£ã¦ãã¾ãã¾ããã
ãã®ããã«ãã©ã¡ã¼ã¿åãã¹ãã¯ãããããã¨ãã¹ãã³ã¼ããèªã¿ã¥ããããæ¬æ¥ã®ãã¹ãã®ç®çã§ããä»æ§ã®è¡¨ç¾ãã§ããªããªã£ã¦ãã¾ãã¾ãã
ãªã®ã§ãã±ã¼ã¹ãã¤ã±ã¼ã¹ã§ãããã©ã¡ã¼ã¿åã®éã¯æ¯ãèããåãã§å
¥åãç°ãªãåå¤ã¯ã©ã¹ãã¨ã«åå²ãã¦è¡ãã®ãè¯ãç²åº¦ã ã¨æãã¾ãããã©ã¡ã¼ã¿åããããã¦æ¬æ¥ã®ç®çãå¿ããªãããã«æ³¨æãã¾ãããã
å®è£ æ¹æ³
ããã¾ã§ã軽ã触ãã¾ãããããã©ã¡ã¼ã¿åãã¹ãã®å
·ä½çãªå®è£
æ¹æ³ãç´¹ä»ãã¾ãã
è²ã
ã¨ä¾¿å©ãªã¢ããã¼ã·ã§ã³ãããã¾ãããæ¬è¨äºã§ã¯å®éã®éçºã§ç¹ã«ä½¿ç¨ãããã®ã«éå®ãã¦ç´¹ä»ãã¾ãã
ãã©ã¡ã¼ã¿åãã¹ãã®å®£è¨
@ParameterizedTest
ãã®ãã¹ãã§ã¯ãã©ã¡ã¼ã¿åãã¹ãããã¾ããï½ã¨ãã宣è¨ãè¡ãã¢ããã¼ã·ã§ã³ã§ãã
ãã©ã¡ã¼ã¿åãã¹ããè¡ã対象ã®ã¡ã½ããã«å¿
ãä»ä¸ãã¾ã
ãã©ã¡ã¼ã¿æå®
åä¸ãã¼ã¿ã®å ¥å
@ValueSource
åºæ¬åã®ã¢ããã¼ã·ã§ã³ãè¡ãã¾ãã以ä¸ã®åãå ¥åå¯è½ã§ãã
ã¢ããã¼ã·ã§ã³ã®ä¸ã«ãã©ã¡ã¼ã¿ã«æ¸¡ãåã¨å¤ãè¨è¿°ãããã¹ãã¡ã½ããã®å¼æ°ã§å¤ãåãåã£ã¦ä½¿ç¨ãã¾ãã
å¤ã¯è¨è¿°ããé çªã«ãã¹ãã¡ã½ããã®å¼æ°ã«æ¸¡ããã¾ãã
ã³ã¼ãä¾
@ParameterizedTest @ValueSource(ints = {0, 5}) public void å¹´é½¢ã0以ä¸5以ä¸ã®å ´å_åå ã¨ãã(int age) { assertEquals("åå ", getGrade(age)); }
ããã§ã仮㫠age=0
ã®ãã¹ãã§ãããå ´åã©ããªãã®ï¼ã¨æã£ãæ¹ãããããããã¾ããã
ãã©ã¡ã¼ã¿åãã¹ãã§ã¯éä¸ã§ãã¹ããããã¦ãå
¨ã¦ã®ãã©ã¡ã¼ã¿ã«ã¤ãã¦å®è¡ãè¡ããã©ã®ãã©ã¡ã¼ã¿ã§ãããããæ確ã«åããã¾ãã
ãªã®ã§JUnitã®ã¢ã³ããã¿ã¼ã³ã§ããã¢ãµã¼ã·ã§ã³ã«ã¼ã¬ãããçºçãããã¨ãããã¾ããã
ã¢ãµã¼ã·ã§ã³ã«ã¼ã¬ãã: ä¸ã¤ã®ãã¹ãã¡ã½ããã«è¤æ°ã®ã¢ãµã¼ããè¨è¿°ããå ´åãéä¸ã§ãã¹ãããããã¨ãã以éã®ãã¹ããå®æ½ãããªããªãã¨ããã¢ã³ããã¿ã¼ã³
åæå
@EnumSource
Enumã®å¤ããã©ã¡ã¼ã¿åã§ãã¾ãã
ä¾ãã° Grade
ã¨ããEnumããã£ãå ´åã@EnumSourceã®å¼æ°ã«Grade.class
ãæå®ãããã©ã¡ã¼ã¿ã« Grade
ã¯ã©ã¹ã®ãªãã¸ã§ã¯ãåãè¨è¿°ãã¾ãã
ã³ã¼ãä¾
enum Grade { KINDERGARTEN("åå "), ELEMENTARY_SCHOOL_STUDENT("å°å¦ç"), JUNIOR_HIGH_SCHOOL_STUDENT("ä¸å¦ç"), SENIOR_HIGH_SCHOOL_STUDENT("é«æ ¡ç"), GROWN_UP("大人"); } int getFee(Grade grade) { if (grade == Grade.KINDERGARTEN || grade == Grade.ELEMENTARY_SCHOOL_STUDENT) { return 0; } return 1000; } @ParameterizedTest @EnumSource(value = Grade.class, names = {"KINDERGARTEN, ELEMENTARY_SCHOOL_STUDENT"}) public void åå ããå°å¦çã®å ´å_æéãç¡æã¨ãã(Grade grade) { assertEquals(0, getFee(grade)); }
è¤æ°ãã¼ã¿ã®å ¥å
@CsvSource
åè¿°ããããã«ä½¿ãã©ããã«ã¯æ³¨æãå¿
è¦ã§ããã
ãã¹ã対象ã®ã¡ã½ãããè¤æ°ã®å¼æ°ãå¿
è¦ã¨ããå ´åãæå¾
å¤ãå¼æ°ã¨é£åãããããªå ´åã«å½¹ç«ã¡ã¾ãã
ã³ã¼ãä¾
@ParameterizedTest @CsvSource({"1901, 1, 1", "2000, 12, 31"}) public void 1901å¹´ãã2000å¹´ã®100å¹´éã¯_20ä¸ç´ã¨ãã(int year, int month, int day) { assertEquals(20, getCentury(year, month, day)); }
ä¸è¨ã®ã³ã¼ããè¦ãã¨stringã§ä¸ãããã©ã¡ã¼ã¿ãintã«å¤æããã¦ãã¾ãã
ãã©ã¡ã¼ã¿åãã¹ãã§ã¯ãã®ãããªæé»çãªå¤æãè¡ã£ã¦ããã¾ãã
æé»çãªå¤æã®ç¨®é¡ã«ã¤ãã¦
ã¾ã¨ã
JUnitã®ãã©ã¡ã¼ã¿åãã¹ãã«ã¤ãã¦ç´¹ä»ãã¾ããã
使ãã©ããããè¦æ¥µããã°ãã¹ãã³ã¼ããæ¸ãã®ã«é常ã«æå¹ãªãã¯ããã¯ã«ãªãã¾ãã
ãã²ä½¿ãããªãã¦ãè¯ãJUnitã©ã¤ããéãã¾ãããï¼
åè
https://oohira.github.io/junit5-doc-jp/user-guide/#writing-tests-parameterized-tests
ã¨ã³ã¸ãã¢ä¸éæ¡ç¨ãµã¤ã
ã©ã¯ã¹ã§ã¯ãã¨ã³ã¸ãã¢ã»ãã¶ã¤ãã¼ã®ä¸éæ¡ç¨ãç©æ¥µçã«è¡ã£ã¦ããã¾ãï¼
ãèå³ããã¾ãããæ¯éã確èªããé¡ããã¾ãã
https://career-recruit.rakus.co.jp/career_engineer/
ã«ã¸ã¥ã¢ã«é¢è«ãç³è¾¼ã¿ãã©ã¼ã
ã©ã®è·ç¨®ã«å¿åããã°è¯ããããããªãã¨ããæ¹ã¯ãã«ã¸ã¥ã¢ã«é¢è«ãéæè¡ã£ã¦ããã¾ãã
以ä¸ãã©ã¼ã ãããç³è¾¼ã¿ãã ããã
rakus.hubspotpagebuilder.com
ã©ã¯ã¹Developersç»é²ãã©ã¼ã
https://career-recruit.rakus.co.jp/career_engineer/form_rakusdev/
ã¤ãã³ãæ
å ±
ä¼ç¤¾ã®é°å²æ°ãç¥ãããæ¹ã¯ãæ¯é±éå¬ãã¦ããã¤ãã³ãã«ãåå ãã ããï¼
âTECH PLAY
techplay.jp
âconnpass
rakus.connpass.com