次ä¸ä»£ã®ã¢ãã¯ãã¬ã¼ã ã¯ã¼ã¯ã§ããJMockitã®åºæ¬çãªä½¿ãæ¹
以åã®ã¢ãã¯ãã¬ã¼ã ã¯ã¼ã¯ã®æè¡çå¶ç´
ä»ã¾ã§ç§ãæ å½ãã¦ããããã¸ã§ã¯ãã«ããã¦ã¯ãã¢ãã¯ãªãã¸ã§ã¯ãã使ã£ãJUnitã®åä½è©¦é¨ã¯jMockã¨EasyMockã®ããããã®ãã¬ã¼ã ã¯ã¼ã¯ãå©ç¨ãã¦è¡ã£ã¦ãã¾ããããããããããã®ãã¬ã¼ã ã¯ã¼ã¯ã¯Javaプラットフォームにおけるコード自動生成の考え方の変遷ã§èª¬æããããã«åçãããã·ã¼ã«åºã¥ãã¦ããããã以ä¸ã®ãããªå¶ç´ãããã¾ããã
- ã¢ãã¯åãã対象ã®åã¯ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã¦ããããç¶æ¿å¯è½ãªã¯ã©ã¹ã§ãããã¨
- ã¢ãã¯åããã¡ã½ããã¯finalãstaticãprivateã§ãªããã¨*1
- ã¢ãã¯åãããã¸ãã¯ã¯ã³ã³ã¹ãã©ã¯ã¿ã¼ã®å¼ã³åºãã§ã¯ãªããã¨
- ã¢ãã¯ãªãã¸ã§ã¯ãããã¹ã対象ã¯ã©ã¹ã«DIããã©ã¡ã¼ã¿ã¼çµç±ã§å¼ã渡ããã¨ãå¯è½ã§ãããã¨
- ã¢ãã¯åããå ´åã¯ã¯ã©ã¹å ¨ä½ãã¢ãã¯åããå¿ è¦ããããã¨ï¼getterãsetterãªã©ã¯æ¬ç©ã®å®è£ ã使ããè¤éãªãã¸ãã¯ãæã¤ç¹å®ã®ã¡ã½ããã®ã¿ãã¢ãã¯åããã¨ãã£ããã¨ã¯ä¸å¯è½ï¼
ãã¡ããããã¹ããã¡ã¼ã¹ãéçºããªãã¡ã¯ã¿ãªã³ã°ã«ãã£ã¦ãããããå¶ç´ãåé¿ããªãã試é¨ããããã³ã¼ããè¨è¿°ãããã¨ã大åãªã®ã§ãããå ´åã«ãã£ã¦ã¯ä¸è¨ã®å¶ç´ããã¾ãã«ã大ããã試é¨ãéãããã ãã®ç®çã§éå°ã«è¤éãªè¨è¨ãå¼·ãããã¦ããã¨ããäºå®ããããã¨ãå¦å®ã§ãã¾ããã
ããã§ç´¹ä»ããJMockitã¨ãããã¬ã¼ã ã¯ã¼ã¯ã使ãã¨ãä¸è¨ã®åé¡ãã»ã¼å®å
¨ã«è§£æ±ºãããã¨ãå¯è½ã§ããJMockitèªä½ã¯ãäºãã«ç¬ç«ããè¤æ°ã®APIããæ§æããã¦ãããå¾æ¥ã®ã¢ãã¯ãã¬ã¼ã ã¯ã¼ã¯ã«æ¯ã¹ãã¨ç¸å½å¤§è¦æ¨¡ã«ãªã£ã¦ãã¾ããããã§ã¯ãåºæ¬çãªä½¿ãæ¹ã¨ä¸è¨ã®åé¡ç¹ãã©ã®ããã«è§£æ±ºãããã®ãã«ã¤ãã¦ã®å
·ä½ä¾ã«çµã£ã¦èª¬æããã¦ããã ãã¾ããJMockitã®å
æ¬çãªèª¬æã¯ããªãè¯ãæ¸ããããã¥ã¼ããªã¢ã«ãããã¾ãã®ã§ãã¡ããåç
§ãã¦ãã ããã
http://jmockit.googlecode.com/svn/trunk/www/tutorial.html
JMockitã®ã¤ã³ã¹ãã¼ã«
ã¨ãããã試ãã¦ã¿ãã ããªãJMockitã®ã¤ã³ã¹ãã¼ã«ã¯é常ã«ç°¡åã§ããå®éã以ä¸ãããã¦ã³ãã¼ããã
Google Code Archive - Long-term storage for Google Code Project Hosting.
é©å½ãªãã©ã«ãã¼ã«å±éãããã¯ã©ã¹ãã¹ã«jmockit.jarã追å ããã ãã§ãããã ããããã§ã®ãã¤ã³ãã¯JUnitã®ã©ã¤ãã©ãªã¼ããå
ã«jmockitãèªã¿è¾¼ã¾ããããã«ããå¿
è¦ãããã¨ããç¹ã§ãããã¨ãã°ãeclipseã®å ´åã
ã®ããã«ãã«ããã¹ã®è¨å®ã§é çªãæå®ãã¾ãããªããJava6ã§ãªãã¦Java5ã®å ´åã¯VMãªãã·ã§ã³ã«-javaagent:jmockit.jarãä»ããå¿
è¦ãããã¿ããã§ããã¾ããJUnit4ã使ãå ´åãã¼ã¸ã§ã³4.5以éãå¿
è¦ãªããã§ãã詳ããã¯ä»¥ä¸ã«èª¬æãããã¾ãã
http://jmockit.googlecode.com/svn/trunk/www/gettingStarted.html
ããããããã«ãããã»ã¹ã«çµã¿è¾¼ãå ´åãAntãMavenã®è¨å®æ¹æ³ã¯ä»¥ä¸ã«èª¬æãããã¾ãã
http://jmockit.googlecode.com/svn/trunk/www/tutorial/RunningTests.html#ant
JMockitã®æãåºæ¬çãªä½¿ãæ¹
JMockitã«ã¯å¤§ããåãã¦ãExpectations APIããVerification APIããAnnotations APIãã®3éãã®APIãç¨æããã¦ãã¾ãããã¨ããããå¾æ¥ã®jMockã®ãããªè©¦é¨ãç½®ãæãããã¨ãèããã®ã§ããã°ãä»ã®2種é¡ã®APIã¯ãã£ããå®å
¨ã«ç¡è¦ãã¦ãExpectations APIãã®ä½¿ãæ¹ã®ã¿ãç解ããã°ååã ã¨æãã¾ãã
ãã¨ãã°ãå
¸åçãªSpringã¢ããªã±ã¼ã·ã§ã³ã®ãµã¼ãã¹å±¤ã§ä»¥ä¸ã®ãããªã¤ã³ã¿ã¼ãã§ã¼ã¹ããã£ãã¨ãã¾ãã
public interface PersonService { void persistPerson(Person person); }
ããã¦ããã®ãµã¼ãã¹ã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã以ä¸ã®ãããªã¯ã©ã¹ããã£ãã¨ãã¾ãã
@Service public class PersonServiceImpl implements PersonService { @Inject private PersonDao personDao; @Override public void persistPerson(Person person) { long id = personDao.persist(person); person.setId(id); } }
ããã§ã¯ããµã¼ãã¹ã¯ã©ã¹ã«PersonDaoã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ããDAOã¯ã©ã¹ãDIããã¦ãããã¾ããDAOã¯ã©ã¹ãPersonãã¼ã¿ãæ°¸ç¶åããéã«æ¡çªãããIDãæ»ãå¤ã¨ãã¦è¿ãã¨ãããã¨ãæ³å®ãã¦ãã¾ãããã®å ´åããã®ãµã¼ãã¹ã¯ã©ã¹ã®åä½è©¦é¨ãè¡ãããã«ã¯ããµã¼ãã¹ã¯ã©ã¹ãä¾åããPersonDaoã®æ¯ãèãã模å£ããã¢ãã¯ãªãã¸ã§ã¯ããä½æããã¤ã³ã¸ã§ã¯ã·ã§ã³ãã¦ããå¿ è¦ãããã¾ãããã®ãããªè©¦é¨ã¯ä¼çµ±çãªã¢ãã¯ãã¬ã¼ã ã¯ã¼ã¯ã§ãç°¡åã«è¨è¿°ã§ãã¾ãããJMockitã®å ´åã以ä¸ã®ããã«è¨è¿°ãããã¨ãã§ãã¾ãã
package test; import static org.junit.Assert.assertEquals; import mockit.Deencapsulation; import mockit.Expectations; import mockit.Mocked; import org.junit.Before; import org.junit.Test; public class PersonServiceImplTest { // ãã¹ã対象ã¯ã©ã¹ PersonServiceImpl target = new PersonServiceImpl(); // ãã¹ã対象ã¯ã©ã¹ã®åä½ã«å¿ è¦ãªãã£ã¯ã¹ã㣠// PersonDaoã®ã¢ãã¯ãã£ã¼ã«ã // ã¢ãã¯ãªãã¸ã§ã¯ãã¯èªåçã«çæããã¦ãã®ãã£ã¼ã«ãã«ã¤ã³ã¸ã§ã¯ã·ã§ã³ãããã @Mocked PersonDao personDao; // ã¢ãã¯åãããªãæ®éã®ãªãã¸ã§ã¯ã Person person = new Person(); @Before public void setUp() throws Exception { // ãã¹ã対象ã¯ã©ã¹ã«ã¢ãã¯DAOãã¤ã³ã¸ã§ã¯ã·ã§ã³ããã // ããã§ã¯JMockitã®ãªãã¬ã¯ã·ã§ã³ã¦ã¼ãã£ãªãã£ã使ã£ã¦privateãã£ã¼ã«ãã«è¨å®ãã¦ããã Deencapsulation.setField(target, personDao); // ãã©ã¡ã¼ã¿ã¼ãªãã¸ã§ã¯ããåæå person.setAge(30); person.setName("test"); } @Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); result = 1L; }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); // ãã¹ãå¼åºãå¾ã®ç¢ºèª assertEquals(1L, (long)person.getId()); // ã¢ãã¯ãªãã¸ã§ã¯ããæå¾ éãå¼ã³åºããããã¨ã¯èªåçã«ããªãã¡ã¤ãããã } }
åºæ¬çã«ä»ã¾ã§jMockãªã©ã使ã£ããã¨ã®ãã人ã§ããã°ãããã«ç解ã§ããå 容ã§ããã¨æãã¾ãã
- ã¢ãã¯åããã対象ã®å(PersonDao)ããã¹ãã¯ã©ã¹ã®ãã£ã¼ã«ãã§å®£è¨ã@Mockedã¢ããã¼ã·ã§ã³ãä»å ããã
- setUp()ã¡ã½ããã§ãã¹ã対象ã¯ã©ã¹ã«ã¢ãã¯ãªãã¸ã§ã¯ããã¤ã³ã¸ã§ã¯ã·ã§ã³ããã
- ãã¹ãã¡ã½ããã®å é ã§Expectationsã®ç¡åå é¨ã¯ã©ã¹ãçæãããã®ã¤ã³ã¹ã¿ã³ã¹åæåãããã¯ä¸ã§ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ããæå¾ åä½ã宣è¨ãããï¼ããã§ã¯personDaoã®persist()ã¡ã½ããã1åã ãå¼ã³åºãããæ»ãå¤ã¯1Lã¨ãªããã¨ã宣è¨ãã¦ãããï¼
- ãã¹ã対象ãªãã¸ã§ã¯ããå®éã«å¼ã³åºãã
- å¿ è¦ã«å¿ãã¦ãã¹ã対象å¼ã³åºãå¾ã®ç¶æ ã確èªããã
ã¨ããæµãã«ãªãã¾ãã
ã¢ãã¯ãªãã¸ã§ã¯ãã宣è¨ãã3種é¡ã®å ´æ
å ã»ã©ã®åºæ¬çãªä¾ã§ã¯ãã¹ãã¯ã©ã¹ã®@Mockedã¢ããã¼ã·ã§ã³ã¤ãã®ãã£ã¼ã«ãã¨ãã¦å®£è¨ãããã¨ã§ãåä½è©¦é¨ã®å®è¡ã«å¿ è¦ãªã¢ãã¯ãªãã¸ã§ã¯ããèªåçã«ä»£å ¥ããããã¨ã説æãã¾ããããã®æ¹æ³ã¯åä¸ãã¹ãã¯ã©ã¹ã®è¤æ°ã®ãã¹ãã¡ã½ããéã§åãã¢ãã¯ãªãã¸ã§ã¯ããå ±æããå ´åã«ã¯ä¾¿å©ã§ããããããç¹å®ã®ãã¹ãã¡ã½ããã®ã¿ã§ä½¿ç¨ãããã¢ãã¯ãªãã¸ã§ã¯ããããå ´åã¯ã以ä¸ã®å¥ã®æ¹æ³ãæä¾ããã¦ãã¾ãã
ãã¹ãã¡ã½ããã®ãã©ã¡ã¼ã¿ã¼ã¨ãã¦ã¢ãã¯ãªãã¸ã§ã¯ãã宣è¨ãã
æ®éãJUnitã®ãã¹ãã¡ã½ããã«ãã©ã¡ã¼ã¿ã¼ã宣è¨ãããã¨ã¯ããã¾ããããJMockitã®å ´åã¯ç¹æ®ãªè¨æ³ã¨ãã¦ã以ä¸ã®ããã«ãã¹ãã¡ã½ããã®ãã©ã¡ã¼ã¿ã¼ã¨ãã¦å®£è¨ãããã¯ã©ã¹ã¯èªåçã«ã¢ãã¯ãªãã¸ã§ã¯ãã¨ãªãã¾ããï¼@Mockedãä»ãã¦ãè¯ããé常ã¯çç¥ããããï¼
@Test public void persistPerson(final PersonDao personDao) { // ãã¹ã対象ã¯ã©ã¹ã«ã¢ãã¯DAOãã¤ã³ã¸ã§ã¯ã·ã§ã³ãã Deencapsulation.setField(target, personDao); // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); result = 1L; }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); // ãã¹ãå¼åºãå¾ã®ç¢ºèª assertEquals(1L, (long)person.getId()); // ã¢ãã¯ãªãã¸ã§ã¯ããæå¾ éãå¼ã³åºããããã¨ã¯èªåçã«ããªãã¡ã¤ããã }
Expectaionsã®å é¨ã¯ã©ã¹ã®ãã£ã¼ã«ãã¨ãã¦ã¢ãã¯ãªãã¸ã§ã¯ãã宣è¨ãã
以ä¸ã®ããã«ãExpectaionsã®å é¨ã¯ã©ã¹ã®ãã£ã¼ã«ãã¨ãã¦å®£è¨ãããã¯ã©ã¹ãèªåçã«ã¢ãã¯ãªãã¸ã§ã¯ãã¨ãªãã¾ããExpectaionsãã¨ã«ãã¼ã«ã«ãªã¢ãã¯ãªãã¸ã§ã¯ããå©ç¨ãããå ´åã«ä¾¿å©ã§ãããã®å ´åã@Mockedã¯çç¥å¯è½ã§ãã
@Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() { PersonDao personDao; { // ãã¹ã対象ã¯ã©ã¹ã«ã¢ãã¯DAOãã¤ã³ã¸ã§ã¯ã·ã§ã³ãã setField(target, personDao); personDao.persist(person); result = 1L; }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); // ãã¹ãå¼åºãå¾ã®ç¢ºèª assertEquals(1L, (long)person.getId()); // ã¢ãã¯ãªãã¸ã§ã¯ããæå¾ éãå¼ã³åºããããã¨ã¯èªåçã«ããªãã¡ã¤ããã }
å³å¯ãªå¼ã³åºãé åºã®ç¢ºèªãä¸è¦ã§åã«ã¹ã¿ãåãããå ´å
@Mockedã®ä»£ããã«@NonStrictã使ãã¨æå®ããåã«å¯¾ããã¢ãã¯ã®å¼ã³åºãé åºã¨å¼ã³åºãåæ°ã«å¯¾ãããã§ãã¯ãããããªããªãã¾ããã¾ããnew Expectations()ã®ä»£ããã«new NonStrictExpectations()ã¨è¨è¿°ãããã¨ã§ãç¹å®ã®æå¾ åä½ã«éå®ãã¦å¼ã³åºããã§ãã¯ãã¯ãããã¨ãã§ãã¾ãã
ã¢ãã¯ãªãã¸ã§ã¯ããè¤æ°åå¼ã³åºãããå ´åã®è¨è¿°æ¹æ³
åä¸ãã©ã¡ã¼ã¿ã¼ã§åãã¢ãã¯ãªãã¸ã§ã¯ããè¤æ°å¼ã³åºãããå ´åãExpectaionsã®å®£è¨ã®å®£è¨ã®ä¸ã§resultãã£ã¼ã«ãã«è¤æ°ã®çµæãåä»£å ¥ãããã¨ã§ãä»£å ¥ããé çªã«ã¢ãã¯ãªãã¸ã§ã¯ãã«ç°ãªãæ»ãå¤ãè¿å´ããããã¨ãã§ãã¾ãã
@Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); result = 1L; result = 2L; result = 3L; }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); assertEquals(1L, (long)person.getId()); target.persistPerson(person); assertEquals(2L, (long)person.getId()); target.persistPerson(person); assertEquals(3L, (long)person.getId()); // ã¢ãã¯ãªãã¸ã§ã¯ããæå¾ éãå¼ã³åºããããã¨ã¯èªåçã«ããªãã¡ã¤ããã }
ãªããå¥ã®æ¸ãæ¹ã¨ãã¦ãresultãã£ã¼ã«ãã«å¯¾ããä»£å ¥ãè¡ã代ããã«returns()ã¡ã½ãããå¼ã³åºã以ä¸ã®å½¢å¼ã§è¨è¿°ãããã¨ãã§ãã¾ãã
@Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); returns(1L, 2L, 3L); }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); assertEquals(1L, (long)person.getId()); target.persistPerson(person); assertEquals(2L, (long)person.getId()); target.persistPerson(person); assertEquals(3L, (long)person.getId()); // ã¢ãã¯ãªãã¸ã§ã¯ããæå¾ éãå¼ã³åºããããã¨ã¯èªåçã«ããªãã¡ã¤ããã }
åããã©ã¡ã¼ã¿ã¼ã§æ»ãå¤ãåãå ´åããããã¯æ»ãå¤ãvoidã®å ´åã¯ä»¥ä¸ã®ããã«timesãã£ã¼ã«ããç¨ãã¦å¼ã³åºãåæ°ã宣è¨ããæ¹æ³ãããã¾ãã
@Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); result = 3L; times = 3; }};ããã ... }
ãã¡ãããã¢ãã¯å¼ã³åºãã®ãã©ã¡ã¼ã¿ã¼ãç°ãªã£ãããè¤æ°ã®ã¢ãã¯ãªãã¸ã§ã¯ããå¼ã³åºãå ´åã¯ãnew Expectations()ã®ä¸ã§é çªã«å¿ è¦ãªåæ°ã ãå¼ã³åºãã¦ãããã¨ãã§ãã¾ããããã«ããã®new Expectations()ã®å¼ã³åºããã¿ã¼ã³ãè¤æ°åç¹°ãè¿ãããå ´åã«ã¯ã以ä¸ã®ããã«Expectationsã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã«æ°å¤ã渡ããã¨ã§ç¹°ãè¿ãåæ°ãæå®ã§ãã¾ãã
@Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations(3) {{ personDao.persist(person); result = 3L; }}; ... }
ã¢ãã¯ãªãã¸ã§ã¯ãã«ä¾å¤ãçºçãããå ´å
ã¢ãã¯ãªãã¸ã§ã¯ãã«ä¾å¤ãçºçããã¦ä¾å¤çºçãã¹ã®è©¦é¨ãè¡ããã¨ã¯ç°¡åã§ããresultãã£ã¼ã«ãã«çºçããããä¾å¤ãä»£å ¥ããã°ããã ãã§ããããã§ã¯3åã®ã¢ãã¯å¼ã³åºãã«å¯¾ãã¦3åç®ã®ã¿ä¾å¤ãçºçãããä¾ãæ¸ãã¦ãã¾ãã
@Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); result = 1L; result = 2L; result = new RuntimeException("test"); }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); assertEquals(1L, (long)person.getId()); target.persistPerson(person); assertEquals(2L, (long)person.getId()); // 3åç®ã®å¼ã³åºãã§ä¾å¤ãçºç try { target.persistPerson(person); } catch (RuntimeException ex) { assertEquals("test", ex.getMessage()); } }
ã¢ãã¯ãªãã¸ã§ã¯ãã®ãã©ã¡ã¼ã¿ã¼ã®ããã¾ããªãããã³ã°
ä»ã¾ã§ã®ä¾ã®ããã«Expectationsãããã¯ã®ä¸ã§ã¢ãã¯ãªãã¸ã§ã¯ããæ®éã«å¼ã³åºããå ´åããã©ã¡ã¼ã¿ã¼ã®ãããã³ã°ã¯equals()æ¯è¼ã¨ãªãã¾ããjMockã«ãããããã«ãããã¾ããªãããã³ã°ãããããã¨ãã§ãã¾ããå
·ä½çãªæ¹æ³ã«ã¤ãã¦ã¯ã以ä¸ãåç
§ãã¦ãã ããã
http://jmockit.googlecode.com/svn/trunk/www/tutorial/BehaviorBasedTesting.html#hamcrest
ã¢ãã¯ã«æ¸¡ããããã©ã¡ã¼ã¿ã¼ã®ä¸èº«ã®ç¢ºèªã¨ãã©ã¡ã¼ã¿ã¼ãªãã¸ã§ã¯ãã®å¤æ´ãè¡ãå ´å
ããããããjMockãªã©ã®ä¼çµ±çãªã¢ãã¯ã©ã¤ãã©ãªã¼ã§ã¯å®è£
ãå°é£ã¨ãªãã¨ããã§ãããjMockitã®æ¬é ãçºæ®ããé åã¨ãªãã¾ãã
ä»ã¾ã§ã®ä¾ã§ã¯PersonDaoãæ»ãå¤ã¨ãã¦IDãè¿ãããã®æ»ãå¤ãPersonServiceImplå´ã§è¨å®ããã¨ããæ³å®ã§è©±ããã¦ãã¾ãããããããå®éã«ã¯DAOã¯ã©ã¹ã®å´ã§IDãæ¡çªããã®ã ãããã¡ã½ããå¼ã³åºãã®å¯ä½ç¨ã¨ãã¦DAOãPersonã«å¯¾ãã¦èªåçã«IDãã£ã¼ã«ããè¨å®ããã¨ãããã¨ã®æ¹ãèªç¶ã§ãããã ãããã®å ´åã¢ãã¯ã®å¼ã³åºãã«ä¼´ãå¯ä½ç¨ã¨ãã¦å¼ã渡ããããã©ã¡ã¼ã¿ã¼ã®ç¶æ
ãå¤æ´ããå¿
è¦ããããããåç´ã«æ»ãå¤ãä¾å¤ãã¢ãã¯ã«è¿å´ãããã¨ããä»ã¾ã§ã®Expectationsã®è¨è¿°ã§ã¯ä¸ååã§ããJMockitã§ã¯ã¢ãã¯å¼ã³åºãã®ç´å¾ã«Delegationã¨å¼ã°ãããªãã¸ã§ã¯ããè¨è¿°ãããã¨ã§ãã¢ãã¯ã«æ¸¡ããããã©ã¡ã¼ã¿ã¼ããã§ãã¯ãããå¤æ´ããããããã¨ãç°¡åã«ã§ããããã«ãªã£ã¦ãã¾ãã*2
@Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); result = new Delegate() { // ãããã¯ã«å¼ã³åºãã®ãã©ã¡ã¼ã¿ã¼ã渡ããã void persist(Person person) { assertEquals(30, person.getAge()); assertEquals("test", person.getName()); person.setId(1L); // ãã©ã¡ã¼ã¿ã¼ã®ç¶æ ãæ¸ãæãããã } }; }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); assertEquals(1L, (long)person.getId()); }
ã¡ã½ãããã©ã¡ã¼ã¿ã¼ã®ç¶æ ãæ¸ãæããã®ã¯Javaã§ã¯ä¸è¬çã§ã¯ããã¾ããããoutãã©ã¡ã¼ã¿ã¼ã¨ãã¦ä½¿ã£ãããCollecting Parameterå®è£ ãã¿ã¼ã³ãå©ç¨ããããããããªå ´åã«ã¯ã¾ãã«ãã©ã¡ã¼ã¿ã¼ã®ç¶æ å¤æ´ãå¿ è¦ã«ãªãã¾ããããã§ç´¹ä»ããæ¹æ³ã使ããã¨ã§ããã®ãããªå ´åã«ãç°¡åã«å¯¾å¦ãããã¨ãã§ãã¾ãã
DAOãDIã§ã¯ãªãã¦ã¡ã½ããå é¨ã§ç´æ¥newããã¦ããå ´å
DIã使ããã¨ã§ãä»ã¾ã§ã®ããã«ã¢ãã¯ãªãã¸ã§ã¯ããã¤ã³ã¸ã§ã¯ã·ã§ã³ãã¦ç°¡åã«è©¦é¨ãããã¨ãã§ãã¾ããããã¡ããããã¯DIã®éè¦ãªã¡ãªãããªã®ã§ãããå ´åãã£ã¦ã¯DIã®ç°å¢ãå©ç¨ã§ããªãã£ãããããããDIã使ããã¨ã大ããã ã£ãããããããªå ´åãããããããã¾ãããä»®ã«PersonServiceImplã®å®è£ ã以ä¸ã®ããã«ãªã£ã¦ããã¨ãã¾ãã
@Override public void persistPerson(Person person) { PersonDao personDao = new PersonDaoImpl(); personDao.persist(person); }
å¾æ¥ãã®ãããªã±ã¼ã¹ããã¡ãã¨åä½è©¦é¨ãããã¨ã¯é常ã«é£ããã£ãã®ã§ãããJMockitã§ã¯ä»¥ä¸ã®ããã«ãããã¨ã§é£ãªã対å¦ã§ãã¾ãã
import static org.junit.Assert.assertEquals; import mockit.Deencapsulation; import mockit.Delegate; import mockit.Expectations; import mockit.Mocked; import org.junit.Before; import org.junit.Test; @SuppressWarnings("unused") public class PersonServiceImplTest { // ãã¹ã対象ã¯ã©ã¹ PersonServiceImpl target = new PersonServiceImpl(); // ãã¹ã対象ã¯ã©ã¹ã®åä½ã«å¿ è¦ãªãã£ã¯ã¹ã㣠// PersonDaoã®ã¢ãã¯ãã£ã¼ã«ã // ã¢ãã¯ãªãã¸ã§ã¯ãã¯èªåçã«çæããã¦ãã®ãã£ã¼ã«ãã«ã¤ã³ã¸ã§ã¯ã·ã§ã³ããã // captureå±æ§ã§æå®ããæ大åæ°ã¾ã§æ°è¦ã«newãããã¤ã³ã¹ã¿ã³ã¹ã§ç½®ã代ãã @Mocked(capture = 1) PersonDao personDao; // ã¢ãã¯åãããªãæ®éã®ãªãã¸ã§ã¯ã Person person = new Person(); @Before public void setUp() throws Exception { // ãã©ã¡ã¼ã¿ã¼ãªãã¸ã§ã¯ããåæå person.setAge(30); person.setName("test"); } @Test public void persistPerson() { // ã¢ãã¯ãªãã¸ã§ã¯ãã«å¯¾ãã¦æå¾ åä½ãå®£è¨ new Expectations() {{ personDao.persist(person); result = new Delegate() { // ãããã¯ã«å¼ã³åºãã®ãã©ã¡ã¼ã¿ã¼ã渡ããã void persist(Person person) { assertEquals(30, person.getAge()); assertEquals("test", person.getName()); person.setId(1L); // ãã©ã¡ã¼ã¿ã¼ã®ç¶æ ãæ¸ãæãããã } }; }}; // ãã¹ã対象ã¯ã©ã¹ã®å¼ã³åºã target.persistPerson(person); assertEquals(1L, (long)person.getId()); } }
ãã®å ´åã®ãã¤ã³ãã¯@Mockedã¢ããã¼ã·ã§ã³ã§captureå±æ§ãæå®ãã¦ããã¨ããã§ãã試é¨å®è¡ä¸ã«æ°ããã¤ã³ã¹ã¿ã³ã¹ãnewãããå ´åã¯ãcaptureã§æå®ãããæ大åæ°ã¾ã§èªåçã«ã¢ãã¯ã§ç½®ãæããããã«ãã£ã¼ã«ãã«å¯¾ãã¦åä»£å ¥ãã¦ããã¾ããæ £ããªãã¨é常ã«ä¸æè°ãªæãããã¾ããããã¤ãã³ã¼ããæ¸ãæãã¦ãã¾ããã¨ã§ãã®ãããªãã¸ãã¯ãå®ç¾ããã¦ãã¾ãã¾ãã
staticã¡ã½ãããfinalã¯ã©ã¹ã®ã¢ãã¯å
JMockitã§ã¯staticã¡ã½ãããfinalãªã¡ã½ãããã¯ã©ã¹ãã¢ãã¯ã§ç½®ãæãã¦ãã¾ãäºããå¯è½ã§ããå½ç¶JDKã®æ¨æºã¯ã©ã¹ã«å¯¾ãã¦ãé©ç¨ã§ãã¾ãã以ä¸ã®ä¾ã¯æ¤è¨¼ç®ç以å¤ã§ã¯ç¡æå³ãªå 容ã§ãããæ¬æ¥ã¯ç©ºã®Listãè¿ãã¹ãemptyList()ã¡ã½ããã«å¯¾ãã¦ã[1,2,3]ã¨ããè¦ç´ ãå«ãã Listãè¿ãããã«Expectationsãè¨è¿°ãããã¨ã§ãå®éã«æ¬ç©ã®Colletionsã®ã¡ã½ããã®åä½ãå¤æ´ãã¦ãã¾ãã
@Mocked final Collections collections = null; @Test public void sampleTest() { new Expectations() {{ Collections.emptyList(); result = Arrays.asList(1,2,3); }}; assertEquals(Arrays.asList(1,2,3), Collections.emptyList()); }
ã¡ãªã¿ã«ã
@Mocked final Collections collections = null;
ã®è¨è¿°ã¯ãã®ãããªã¤ã³ã¹ã¿ã³ã¹ã®åå¨ããªãUtilsã¯ã©ã¹ãã¢ãã¯åããéã®ã¤ãã£ãªã ã§ããfinal宣è¨ããnullãä»£å ¥ãã¦ãããã¨ã§ç¡é§ã«ã¢ãã¯ãªãã¸ã§ã¯ãã®ã¤ã³ã¹ã¿ã³ã¹ãçæããªãåä½ã¨ãªãã¾ãã
é¨åçãªã¢ãã¯å
ã¯ã©ã¹ãã¢ãã¯åããå ´åã«ãgetterãsetterãªã©ééãããã®ãªãé¨åã®å®è£ ã¯ã¢ãã¯ã«ããæ¬ç©ã®å®è£ ãå©ç¨ããªãããé¨åçãªãã¸ãã¯ã®é¨åã®ã¿ãã¢ãã¯ã¨ãã¦ç½®ãæãããå ´åãããã¾ãããããããã¨ã§getterã®å¼ã³åºããªã©ãããããExpectationã¨ãã¦å®£è¨ããå¿ è¦ããªããªãã¾ããé¨åçãªã¢ãã¯åã«å¯¾ãã¦ã¯ä»¥ä¸ã®2種é¡ã®æ¹æ³ãæä¾ããã¦ãã¾ãã
éçãªé¨åã¢ãã¯å
ã¢ãã¯åããç¯å²ãéçã«ããããã決ã¾ãå ´åã¯ã以ä¸ã®ããã«@Mockedã¢ããã¼ã·ã§ã³ã®methodså±æ§ãvalueå±æ§ã§ã¢ãã¯å¯¾è±¡ã®ã¡ã½ããï¼inverseæå®ããã°è«çãé転ãã¦ã¢ãã¯å¯¾è±¡å¤ã®ã¿æå®ãããã¨ãå¯ï¼ãæå®ã§ãã¾ãããã¨ãã°ã以ä¸ã®ä¾ã§ã¯Personã¨ããPOJOã®sayHello()ã¡ã½ããã®ã¿ã¢ãã¯ã§ç½®ãæãã¦ãã¾ãããgetterãsetterã®ãã¸ãã¯ã¯æ¬ç©ã®ã¯ã©ã¹ã®å®è£ ãæµç¨ãã¦ãã¾ãã
@Mocked("sayHello") Person person; @Test public void sampleTest() { person.setAge(30); person.setName("test"); new Expectations() {{ person.sayHello(); result="Hello World"; }}; assertEquals("Hello World", person.sayHello()); assertEquals(30, person.getAge()); assertEquals("test", person.getName()); }
ãªãã対象ã¡ã½ããã¯è¤æ°æå®ã§ããã¾ãæ£è¦è¡¨ç¾ã®ãããªãã¿ã¼ã³ããããå¯è½ã§ãã詳ããã¯ä»¥ä¸ã®åç
§ãã¦ãã ããã
http://jmockit.googlecode.com/svn/trunk/www/tutorial/BehaviorBasedTesting.html#staticPartial
åçãªé¨åã¢ãã¯å
ã¢ãã¯åãã対象ãéçã«æ±ºã¾ããªãå ´åã以ä¸ã®ããã«Expectationã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã«ã¢ãã¯å¯¾è±¡ã®ã¯ã©ã¹ããã¤ã³ã¹ã¿ã³ã¹ãæå®ããè¨æ³ãå©ç¨ã§ãã¾ãã以ä¸ã¯ã¤ã³ã¹ã¿ã³ã¹ãæå®ããå ´åã§ãã
@Test public void sampleTest() { final Person person = new Person(); person.setAge(30); person.setName("test"); new Expectations(person) {{ // ã³ã³ã¹ãã©ã¯ã¿ã§é¨åã¢ãã¯å対象ãæå®ï¼ã¤ã³ã¹ã¿ã³ã¹æå®ï¼ person.sayHello(); result="Hello World"; }}; assertEquals("Hello World", person.sayHello()); assertEquals(30, person.getAge()); assertEquals("test", person.getName()); }
ä¸æ¹ã以ä¸ã¯ã¯ã©ã¹ãæå®ããå ´åã§ãã
@Test public void sampleTest() { final Person person = new Person(); person.setAge(30); person.setName("test"); new Expectations(Person.class) {{ // ã³ã³ã¹ãã©ã¯ã¿ã§é¨åã¢ãã¯å対象ãæå®ï¼ã¯ã©ã¹æå®ï¼ person.sayHello(); result="Hello World"; }}; assertEquals("Hello World", person.sayHello()); assertEquals(30, person.getAge()); assertEquals("test", person.getName()); }
ã¯ã©ã¹æå®ã®å ´åã¯ãã®ç¹å®ã®ã¯ã©ã¹ã®ã¡ã½ããã®ã¿ãã¢ãã¯åã®å¯¾è±¡ç¯å²ã¨ãªããã¤ã³ã¹ã¿ã³ã¹æå®ã®å ´åã¯ç¶æ¿ããã¦ãããã®ãå«ãã¦ãã¹ã¦ã®ã¡ã½ãããã¢ãã¯åã®å¯¾è±¡ã¨ãªãã¾ãããããã®å ´åãExpectationsã®ãããã¯ã®ä¸ã§å®éã«å¼ã³åºãããã¡ã½ããã®ã¿ãã¢ãã¯å®è£ ã§ç½®æããããã以å¤ã¡ã½ããã¯æ¬ç©ã®å®è£ ãå©ç¨ããããã¨ã«ãªãã¾ãã
*1:ããããéä»®æ³ã¡ã½ãããã¢ã¼ãªã¼ãã¤ã³ãã£ã³ã°ã®ã¡ã½ããã
*2:Delegationãªãã¸ã§ã¯ãã¯Rubyã«ããããããã¯ãGroovyã«ãããã¯ãã¼ã¸ã£ã®ä»£ããã¨æãã°ããã§ããã¾ã æªç¢ºèªã§ããå®éGroovyä¸ã§ã¯ãã¼ã¸ã£ã¼ã使ã£ã¦ç°¡æ½ã«è¨è¿°ãããã¨ã¯å®¹æã ã¨æãã¾ãã