ããã€ãã®å€éšã³ã³ããŒãã³ãã䜿çšããã³ãŒãããã¹ããããšãã«ãã¢ãã¯ãªããžã§ã¯ãã¢ãããŒãã䜿çšããããšããããããŸãã ãŸã ç¥ããªã人ã®ããã«ãç°¡åã«èª¬æããŸãããããã¯äœ¿çšãããã³ã³ããŒãã³ããšåãã€ã³ã¿ãŒãã§ãŒã¹ãæã€ãªããžã§ã¯ãã§ããããã®åäœã¯ãã¹ãã§å®å
šã«æå®ãããŠããããã®äœ¿çšã«ããã¢ããªã±ãŒã·ã§ã³ã®å®è¡ã«å¿
èŠãªã€ã³ãã©ã¹ãã©ã¯ãã£å
šäœãäžããããšãåé¿ã§ããŸãã ããã«éèŠãªããšã¯ãã³ãŒããç¹å®ã®åŒæ°ã䜿çšããŠã¢ãã¯ãªããžã§ã¯ãã®ç¹å®ã®ã¡ãœãããåŒã³åºãããšãç°¡åãã€èªç¶ã«å¶åŸ¡ã§ããããšã§ãã
ãã®èšäºã§ã¯ãJavaã§ã¢ãã¯ãæäœããããã®2ã€ã®äžè¬çãªã©ã€ãã©ãªãŒã§ãã
EasyMockãš
JMockã®æ¯èŒåæã
è¡ããŸãã
JUnitã®åºæ¬çãªç¥èã¯ç解ã«ååã§ããããã®èšäºãèªãã åŸããããã®ã©ã€ãã©ãªã®äž¡æ¹ã䜿çšããæ¹æ³ã«ã€ããŠéåžžã«è¯ãã¢ã€ãã¢ãåŸãããšãã§ããŸãã
åé¡ã®ã¿ã¹ã¯
ãã¹ãããå¿
èŠããããã®ã®äŸãšããŠãããã次ã®æ§é ãæã€ã¢ããªã±ãŒã·ã§ã³ãæ€èšããŸãã
1 2 3 4 5 6 7 8 9
| public class WayTooComplexClass { public WayTooComplexClass(String serverAddress) {} public boolean save(long id, String data) {} public String get(long id) {} } |
çç¥èšå·ã«ãã£ãŠé ãããå®è£
ã§ãã¹ãã¬ãŒãžãšããŠåçŽãªHTTP APIïŒããšãã°ã
elliptics ïŒã䜿çšããããçš®ã®ãµãŒãã¹ã䜿çšã§ããããã«ããŸãã ãã®ãµãŒããŒãåžžã«ãã¹ãã®ããã«ã©ããã«ä¿ç®¡ããã«ã¯ãå°ãªããšã2ã€ã®åé¡ããããŸãã
- ãã¹ããå®è¡ãããã¹ãŠã®ãã·ã³ãããã®ãµãŒããŒã«ã¢ã¯ã»ã¹ã§ããå¿
èŠããããŸãã
- ã¢ãã¯ãµãŒããŒã®åäœã®èª¬æã¯ã³ãŒãã®å€éšã«ãããããç¹ã«1人ã®éçºè
ãã³ãŒããšãµãŒããŒã§ãã¹ããæŽæ°ãããã1人ã®éçºè
ãæŽæ°ããªãå ŽåãããŸããŸãªãã©ãã«ãçºçããå¯èœæ§ããããŸãã
ãã®æç¹ã§ã
ãã³ãŒãã¯ãŠããããã¹ãã«ã¯è€éãããŸããâ¢ã§ãããšèšã£ãŠããããããŠãããæžã蟌ã¿ã§è©°ãŸã£ãŠããŸãã 幞ããªããšã«ãç§ãã¡ã¯ãããã®1ã€ã§ã¯ãªããããå¿
èŠãªèŠæ±ã«é©åã«å¿çããå°ããªHTTPãµãŒããŒããã¹ãããçŽæ¥çæããŸãã ãã®äŸã§ã¯ããã®ãããªç®çã§
æ¡æ©ã䜿çšããŸããã ãã®ãããªã³ãŒãã¯ãäž¡æ¹ã®ã¢ãã¯ã©ã€ãã©ãªã䜿çšããå Žåã«äžè¬çã§ãã
ãã¹ãçšã®æš¡æ¬ãµãŒããŒïŒèªãããšãã§ããŸããïŒ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | public class WayTooComplexClassTest { static interface RequestHandler { String handle(String target, String data); } private static class MockHttpServerHandler extends AbstractHandler { private RequestHandler handler; public void setRequestHandler(RequestHandler handler) { this.handler = handler; } @Override public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch ) throws IOException, ServletException { String req = IOUtils.toString(request.getInputStream(), "UTF-8"); String result = handler.handle(target, req); response.setStatus(HttpStatus.ORDINAL_200_OK); response.setContentType("text/plain"); final ServletOutputStream outputStream = response.getOutputStream(); try { outputStream.print(result); } finally { outputStream.close(); } } } private static final MockHttpServerHandler SERVER_HANDLER = new MockHttpServerHandler(); @BeforeClass public static void startServer() throws Exception { org.mortbay.jetty.Server server = new org.mortbay.jetty.Server(); server.setHandler(SERVER_HANDLER); server.start(); } private final WayTooComplexClass wayTooComplex = new WayTooComplexClass("http://localhost/9001"); |
ããã«ã¯ã3ã€ã®èå³æ·±ããã€ã³ãããããŸãã 1è¡ç®ã¯3ã5è¡ç®ã§ã
RequestHandler
ã€ã³ã¿ãŒãã§ã€ã¹ã説æãã
RequestHandler
ã€ã³ã¿ãŒãã§ã€ã¹ã瀺ããŠããŸãïŒ
ããšãã°ãã¢ãã¬ã¹httpïŒ // habrahabr .ru / blogs / java / 136466 /ã¿ãŒã²ããã¯ããŒã«ã/ blogs / java / 136466 /ã«ãªããŸã ïŒããªã¯ãšã¹ãæ¬æã§ãŠãŒã¶ãŒãéä¿¡ããããŒã¿ã 2è¡ç®ã¯ã7è¡ç®ãã32è¡ç®
MockHttpServerHandler
ã¯ã©ã¹ã§ãã
RequestHandler
ã€ã³ã¹ããŒã«ããããããžãã¹ããžãã¯ãå
šäœãå§ä»»ããããã®äœæ¥ã®çµæãHTTPå¿çã«èšé²ãããŸãã 3è¡ç®ïŒ36ã41è¡ç®ïŒã¯
startServer
ã¡ãœããã§ãããã®ã¡ãœããã¯ãã¢ãããŒã·ã§ã³ãšååããæšæž¬ã§ããããã«ããã®ã¯ã©ã¹ã«ãªã¹ããããŠãããã¹ããå®è¡ãéå§ããŠHTTPãµãŒããŒãéå§ããåã«åŒã³åºãããŸãã
æåã®æãç°¡åãªãã¹ã
çè«çã«ã¯ãsaveã¡ãœããã«é ãããã³ãŒããURL
{serverAddress}/upload/{id}
ãééããããã«
data
ãæž¡ããšä»®å®ããŸãã ãããå®éã«èµ·ãã£ãŠãããã©ããã確èªããŠãã ããã
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Mockery context = new JUnit4Mockery(); @Test public void testSaveWithJMock() { final long id = 13; final String data = "nanofilters"; final RequestHandler requestHandler = context.mock(RequestHandler.class); context.checking(new Expectations() {{ one(requestHandler).handle("/upload/" + id, data); will(returnValue("Saved " + id)); }}); SERVER_HANDLER.setRequestHandler(requestHandler); wayTooComplex.save(id, data); context.assertIsSatisfied(); } |
æåã®è¡ã§ã¯ããã¹ããå®è¡ããJMockã³ã³ããã¹ããäœæããå¿
èŠããããŸãã ãã®ã³ã³ããã¹ãã¯ãã¹ãå
šäœã«ã¯åå
ã§ããããããªãã§ã¯ã§ããŸãã ã åãã¢ãã¯ã®ããã€ãã®åé¡ãåé¿ããããã«ãåãã¹ãã®åã«ã³ã³ããã¹ããåäœæããå¿
èŠããããŸãïŒã€ãŸãã
@Before
ã¢ãããŒã·ã§ã³ã§ããŒã¯ãããã¡ãœããå
ïŒ
@Before
è¡ç®ã§ã¯ãã€ã³ã¿ãŒãã§ã€ã¹ã®ã¢ãã¯ãç°¡åãã€èªç¶ã«äœæããŸãã 次ã«ã10ã13è¡ç®ã§ãã©ã®åŒã³åºããçºçãããã説æããŸãã äžèŠæ§æ
ã¯ããŸãçŽæçã§ã¯ãã
ãŸããããæéã
çµãŠã°æ
£ããŸãã 11è¡ç®ã§ã¯ãåŒæ°
("/upload/" + id)
ããã³
(data)
("/upload/" + id)
ããŠ
handle
ã¡ãœããã1åã ãåŒã³åºãããšãæ³å®ããŠããŸãã 12è¡ç®ã§ã¯ãæåŸã®åŒã³åºããå€
("Saved " + id)
ãè¿ããšèšããŸãã ããã§ã¯ããæ³åã®ãšããã
äžè¬çãªã»ãã¥ãªãã£ã¯ãããŸãã ã 誀ã£ãŠééã£ãåã®å€ãããã«æž¡ããshlopotatäŸå€ã䜿çšããŠãå®è¡æã«ã®ã¿ãããç¥ãããšãã§ããŸãã ãã ããæ»ãå€ãéèŠã§ãªãå ŽåãããããŸã£ããèšè¿°ã§ããŸãããJMockã¯èªåçã«ããã©ã«ãå€ïŒ
0
ã
false
ã
null
ãŸãã¯ç©ºã®æååïŒãè¿ããŸãã 次ã«ãæ°ããäœæããã¢ãã¯ãã³ãã©ãŒã䜿çšãããã¹ã察象ã®ã¢ããªã±ãŒã·ã§ã³ãåŒã³åºããŠã19è¡ç®ã§ãã¹ãŠã®äºæ³ãããåŒã³åºããè¡ãããããšã確èªããå¿
èŠãããããšãã¢ãã¯ãµãŒããŒã«äŒããŸãã ãã¹ãã¯ã©ã¹
@RunWith(JMock.class)
ã¢ãããŒã·ã§ã³
@RunWith(JMock.class)
ãè¿œå ããããš
ã§ãåŸè
ãåãé€ãããšãã§ããŸãã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | IMocksControl control = EasyMock.createControl(); @Test public void testSaveWithEasyMock() { final long id = 15; final String data = "cold fusion reactor"; final RequestHandler requestHandler = control.createMock(RequestHandler.class); expect(requestHandler.handle("/upload/" + id, data)).andReturn("Saved " + id); control.replay(); SERVER_HANDLER.setRequestHandler(requestHandler); wayTooComplex.save(id, data); control.verify(); } |
æåã®è¡ã§ã¯ãJMockã³ã³ããã¹ãã«é¡äŒŒããã³ã³ãããŒã«ãäœæããŸãã 次ã«ã8è¡ç®ã§ã¢ãã¯ãªããžã§ã¯ããäœæããŸãã10è¡ç®ã§ã¯ãç¹å®ã®åŒæ°ã䜿çšããŠhandleã¡ãœãããåŒã³åºãããã®å Žåã¯ç¹å®ã®å€ãè¿ãããšã瀺ããŸãã ããã«
ã¯å
žåçãªã»ãã¥ãªãã£ããããŸããString以å€ã®åã®åŒæ°ãandReturnã«æž¡ãããšãããšãã³ã³ãã€ã«ãšã©ãŒãçºçããŸãã
EasyMockã§ã¯ãç§ã®èŠ³ç¹ããã
äºæ³ãããåäœã®ä»æ§ãããæ確ã«ãªã£ãŠããŸãã ãã ãããã®ã¢ãããŒãã«ã¯æ¬ ç¹ããããŸãã11è¡ç®ã§ãããããã«
ãäºæ³ãããåäœã®èšé²ãçµäºããããšãæ瀺çã«ç€ºãå¿
èŠããããŸã ã ãã¹ãŠã®ãããžãã¹ããžãã¯ãã®åŸã17è¡ç®ã«é²ã¿ãäºæ³ããããã¹ãŠã®ã¡ãœãããåŒã³åºãããããšã確èªããŸãã ã¡ãªã¿ã«ãã¡ãœãããäœãè¿ãããæ°ã«ããªãå Žå
ãvoidã¡ãœããã®å Žåã¯ã expect
ã³ã³ã¹ãã©ã¯ãã
çç¥ããŠã requestHandler.handle("/upload/" + id, data)
åŒã³åºãã ãã§ãã ããã«ã
control
䜿çšã¯ãªãã·ã§ã³ã§ãããç°¡åã«ãããå®è¡ã§ããŸãã
1 2 3 4 5 | final RequestHandler requestHandler = EasyMock.createMock(RequestHandler.class); |
ã¡ãœããåŒã³åºãã«å¯Ÿããããè€éãªå¿ç
ããã§ãå€éšã³ã³ããŒãã³ãã«é害ãçºçãããšãã«ã¢ããªã±ãŒã·ã§ã³ãæ£ããåäœããããšããã¹ãããå¿
èŠããããšããŸãã ãããè¡ãã«ã¯ã
handle
ã¡ãœããã§äŸå€ãæŽçããã ãã§ååã§ãããã®åŸãjettyã¯httpã¹ããŒã¿ã¹500èªäœãèšå®ããŸãã
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 8 | @Test public void testErrorHandlingWithJMock() { |
è€éãªããšã¯äœããããŸããã ã³ã¡ã³ãã¯äžèŠã ãšæããŸãã
returnIterator
ãã®ãŸãŸè¿œå ããããšãã§ããŸããã
Action
ã€ã³ã¿ãŒãã§ãŒã¹ãå®è£
ããããšã§ç¬èªã«èª¿æŽããããšãã§ããŸãã 確ãã«ãããã¯
ããŸãæ確ã§ã¯ãªããããã¥ã¡ã³ãã¯ããã»ã©ç±ããããŸãã ã
ã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 | @Test public void testErrorHandlingWithEasyMock() { |
ããã®ãã¹ãŠãã·ã³ãã«ã§ãããããããçãããã«ãã¡ãœãããäœãè¿ããªãå ŽåïŒã€ãŸãvoidåãæã€å ŽåïŒãå¥ã®æ¹æ³ã§èšè¿°ããå¿
èŠãããã
ã¹ã¿ã€ã«ã®ã³ãã¥ããã£ã¯å€±ãããŸãã 次ã®ããã«ãªããŸãã
1 2 3 4 5 6 | @Test public void testErrorHandlingWithEasyMock() { |
andThrow
å ããŠã
andThrow
䜿çšããããšãã§ã
andDelegateTo
ãããã¯ãã
andDelegateTo
ã
ã¡ãœããåŒã³åºããä»ã®ãªããžã§ã¯ãã«
å§ä»»ããŸã ïŒ
ã³ã³ãã€ã«äžã«äžè¬çãªã»ãã¥ãªãã£ã¯ãããŸããïŒ ïŒAnd
andAnswer
ã åŸè
ã®å Žåã
IAnswer
ã€ã³ã¿ãŒãã§ã€ã¹ãå®è£
ãã
answer
ã¡ãœããå
ã«ã³ãŒããèšè¿°ããå¿
èŠããããŸãã JMockãããå°ãé»åãå°ãªãã§ãããã¯ããã«äŸ¿å©ã§ãã
ã¡ãœãããåŒã³åºããšãã®åŒæ°ã®äžèŽ
ããã§ãã¢ãã¯ãããã¡ãœããã®åŒã³åºãã«ã©ã®åŒæ°ãæž¡ãã¹ããæ£ç¢ºã«ã¯ããããªããšããŸãã å¯äžç¢ºå®ãªããšã¯ã
target
åŒæ°ã«ã¯ã©ããã«
id
ãå«ãŸããŠããå¿
èŠããããšããããšã§ãã
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 | @Test public void testArgumentMatchingWithJMock() { |
JMock
ã¯ãã ã¯ã¬ã¹ããããã£ãŒã䜿çšããå¿
èŠã«å¿ããŠéæšæºã®ãã©ãã¯ãžã£ãã¯ãè¿œå ããŠãããã£ãŒãäœæã§ããŸãã
ã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 | @Test public void testArgumentMatchingWithEasyMock() { |
ç¬èªã®ãããã£ãŒã䜿çšããããã
æ¢è£œã®ãããã£ãŒã®ã»ããã¯å°ãããªããŸãã ãã ãããã§ã«ååšãããã®ã¯éåžžã«ããè¡ããããã¹ãŠã®åºæ¬çãªããŒãºãæºãããŸããæ®ãã¯ãèªåã§å¿
èŠãªãããã£ãŒãå®è£
ããããšã§ãJMockãšåæ§ã«æºããããšãã§ããŸãã
åŒã³åºãåæ°
ããã§ãæ¡ä»¶ãå°ãå€æŽããŠãããã€ãã®ã¡ãœãããæ°ååŒã³åºãå¿
èŠããããšèšã£ãŠã¿ãŸãããã ãã®äŸã¯çŸå®ããããªãé ããããŸããããåçŽãªãã®ã瀺ãããã«ããéèŠãªãã®ãçºæããããããŸããã
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 | @Test public void testMultipleInvocationsWithJMock() { |
æ°é
ãã®ããèªè
ãæšæž¬ã§ããããã«ãåã®ãã¹ãŠã®äŸã§ãåèª
one
ã¯ããã ãã§ã¯ãªããäºæ³ãããåŒã³åºãã®æ°ã瀺ããŠããŸããã å¿
èŠãªã³ãŒã«ã®æ°ã¯ãã¹ãŠãµããŒããããŠããŸãïŒãŒãã§ãã£ãŠããããã€ã§ãã£ãŠãã
ã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 | @Test public void testMultipleInvocationsWithEasyMock() { |
ããã§ãããã¹ãŠãéåžžã«åçŽã§ãããããããªãã€ãã¹ããããŸãã
äžããã®å¶éãªãã«ãå°ãªããšãnåããšèšãæ¹æ³ã¯ãããŸãã ã
atLeastOnce
ã¡ãœããã
atLeastOnce
ããšãèãããšãããã¯å°ãå¥åŠã§ãã å°ãªããšã3ã€ã®åŒã³åºããæåŸ
ããã«ã¯ã次ã®ã³ãŒãã®ãããªãã®ãæžãå¿
èŠããããŸãã
1 2 3 4 5 6 7 | @Test public void testMultipleInvocationsWithEasyMock() { |
ã¹ã¿ã
ã¡ãœããããã€ã©ã®ããã«åŒã³åºããããã¯ååãšããŠããŸãéèŠã§ã¯ãªãããšããããããŸãããã¡ãœãããååšããŠäœããè¿ãã®ã¯èå³æ·±ãããšã§ãã
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 8 | @Test public void testStubMethodsWithJMock() { |
ããã¯ãåæ°ã決å®ããã®ãšåãæ¹æ³ã§è¡ãããããšã泚ç®ã«å€ããŸãã
allowing
代ããã«
allowing
ignoring
ãããšèšãããšãã§ããŸãã ãŸããå¿
èŠã«å¿ããŠã1ã€ã®å€§ããªã¹ã¿ãã§Mockãªããžã§ã¯ãå
šäœãç°¡åã«äœæã§ããŸãã
1 2 3 4 5 6 7 | @Test public void testStubObjectsWithJMock() { |
ã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 | @Test public void testStubMethodsWithEasyMock() { |
ã¹ã¿ã€ã«ãããåäžã§ã¯ãããŸããããéåžžã«å¿«é©ã§ãã ãã ãããªããžã§ã¯ããäœæãããšãã«ã®ã¿ãªããžã§ã¯ããã®ã£ã°ã«ããããšãã§ããŸãã
1 2 3 4 5 | @Test public void testStubObjectsWithEasyMock() { final RequestHandler requestHandler = createNiceMock(RequestHandler.class); |
ã¡ãœããã¯ãåŒã³åºããããšããã®ã¿ã€ãã®ããã©ã«ãå€ïŒ
0
ã
false
ãŸãã¯
null
ïŒãè¿ããŸãã
String
ãå«ãnull
ãè¿ããnull
ã ããã®ã¯ã©ã¹ã®JMockã§ã¯ãããã©ã«ãå€ã¯ç©ºã®æååã§ãã
ã¡ãœããåŒã³åºãé åºã®ç¢ºèª
å€ãã®å Žåãã¡ãœãããåŒã³åºãããé åºã¯éèŠã§ããããšãå€æããŠããŸãã åŠæ³ã«é¥ã£ããšä»®å®ãããã¡ã€ã«ãããŠã³ããŒãããçŽåŸã«ãããŠã³ããŒãããŠåç
§ãã¡ã€ã«ãšæ¯èŒããããšã«ããŸããã ã¢ããªã±ãŒã·ã§ã³ãå®éã«ãããè¡ãããšã確èªããã«ã¯
ïŒããã§ãªãå ŽåãããŸãä¿¡é ŒããŸãã...ïŒ ããããè¡ãããšãã§ããŸãïŒ
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Test public void testStrictOrderingWithJMock() { |
ããã§ã¯ãå®éã«å®è¡é åºã確èªãã9è¡ç®ãš13è¡ç®ã«èå³ããããŸãã
inSequence
ã¯ãã¢ãã¯ãªããžã§ã¯ããžã®åŒã³åºãã®é åºã®ã¿ãç£èŠãããã®çŽåŸã«ããã瀺ãããããšãèŠããŠ
inSequence
ããšãéèŠã§ãã ãããã£ãŠã10åã®ç°ãªãåŒã³åºããå³å¯ãªé åºã§è¡ãããããšã远跡ããã«ã¯ã
inSequence
10åèšè¿°ããå¿
èŠ
ããããŸãã ãããã
ããªãã¯äžåºŠã«ããã€ãã®ã·ãŒã±ã³ã¹ã§é»è©±ãåãããšãã§ããŸã ã
ã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 7 | @Test public void testStrictOrderingWithEasyMock() { |
ããã§ã¯ããã¹ãŠãããåçŽ
ã«ãªããŸãã ã
åã¢ãã¯ããšã«ãã¹ã
ãæ°åãªã³/ãªãã§ããŸã ã ããã«ã
control
å
šäœãå³å¯ã«control
ããšãã§ã ïŒæåã®äŸãåç
§ïŒããã®åŸãå«ãŸãããã¹ãŠã®ã¢ãã¯ã®äžè¬çãªé åºããã§ãã¯ãããŸãã ããã«ã
createStrictMock
ãšèšãããšã§
ãäœææã«ããã«ã¢ãã¯ãŸãã¯å¶åŸ¡
ãå³å¯ã«ããããšãã§ããŸã ã ãã¡ããããã®ãããªéã®ç·ã¯æ·±å»ãªãã€ãã¹ã§åžéããå¿
èŠããããŸãã1ã€ã®ã¢ãã¯ïŒãŸãã¯ã³ã³ãããŒã«ïŒ
ãè€æ°ã®ã·ãŒã±ã³ã¹ã«åå ããããšã¯ã§ããŸãã ã ãã®ãããªããšãããããŸããã
ã¡ãœããåŒã³åºããèš±å¯ãããæ¡ä»¶
å Žåã«ãã£ãŠã¯ãã¢ããªã±ãŒã·ã§ã³ã®ç¶æ
ãã·ãã¥ã¬ãŒãããå¿
èŠããããŸããããã¯ãããã€ãã®ã¡ãœãããåŒã³åºããããšå€åããä»ã®ã¡ãœããïŒãŸãã¯åãã¡ãœããïŒãåŒã³åºããããšãã§ãã¯ãããŸãã ã¢ããªã±ãŒã·ã§ã³ã2ã€ã®æ¥åã€ã³ã¹ã¿ã³ã¹ïŒã€ã³ã¹ã¿ã³ã¹ã¯panolaããã³yarboãšåŒã°ããŸãïŒã®ããããã«ç§»åããŠãã¡ã€ã«ãããŠã³ããŒãããããŠã³ããŒãããåãã€ã³ã¹ã¿ã³ã¹ããããŠã³ããŒãããå¿
èŠããããŸãã ããã確èªã§ããŸãã
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Test public void testStatesWithJMock() { |
9è¡ç®ã13è¡ç®ã17è¡ç®ãããã³21è¡ç®ãèŠãå¿
èŠããããŸããããããã®è¡ã§ãã¢ããªã±ãŒã·ã§ã³ãæ£ããç¶æ
ã«ãªã£
when
ã確èªããthenã䜿çšããŠæ°ããã¢ããªã±ãŒã·ã§ã³ãèšå®ããŸãã
ãšãŠãå¿«é© ã ãªãŒãããã³ããã°ã©ãã³ã°ãã©ãã€ã ã®æ¯æè
ã¯ãããããèªåã®å€¢ããã¹ãããæ¹æ³ãèŠã€ãããšèããŠããŸãã
EasyMockïŒ ã¢ããã°ãªã
ãã«ãã¹ã¬ããã³ãŒããã¹ã
äžè¬çã«ããã«ãã¹ã¬ããã³ãŒãã®ãã¹ãã¯éåžžã«å°é£ã§ããç¹å®ã®ã¹ã¬ããã1ã€ãŸãã¯å¥ã®ã¢ã¯ã·ã§ã³ãå®è¡ããã¿ã€ãã³ã°ã«ã¯ããŸããŸãªçµã¿åãããããããã§ãã ç¹ã«ããã¹ãŠãã¯ãªãã£ã«ã«ã»ã¯ã·ã§ã³ãšããŠå®£èšãããæå°éã®ããã¯ã§å¯ŸåŠããããšããå Žåã ç§ãã¡ã¯ä»ããã°ãããã¢ããªã±ãŒã·ã§ã³ããæœè±¡åããããŸããŸãªã©ã€ãã©ãªãŒã§ã®ãã«ãã¹ã¬ããã®ãµããŒããã©ã®ããã«è¡ãããŠããããçŽæ¥èŠãŠãããŸãã
ãžã§ã¢ãã¯
æ£çŽãªãšãããç§ã«ãšã£ãŠããã¯ã²ã©ãèŠçã§ããïŒãã®åé¡ã«é¢ããããã¥ã¡ã³ã
ïŒãšã«ããã
ä»ã®å€ãã®ãã®ã«ã€ããŠïŒã¯ããªãæ¯figçã§ããããããã£ãŠãç§ã¯è©Šè¡é¯èª€ã§è¡åããªããã°ãªããŸããã§ãã
ãããã»ã©åçŽã§ã¯ãããŸããã§ããïŒ ã ãã®çµæãæåã§ããŠã³ããŒãããããŒãžã§ã³ã®hamcrest-core 1.3.0RC1ãšæ¬¡ã®ã³ãŒãã䜿çšããŠãæåã§ããŠã³ããŒãããããŒãžã§ã³ã®JMock 2.6.0-RC2ã«ãã£ãŠä¿åãããŸããã
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private Synchroniser synchroniser = new Synchroniser(); private Mockery context = new JUnit4Mockery() {{ this.setThreadingPolicy(synchroniser); }}; @Test public void testConcurrencyWithJMock() { |
ããã§æåã®è¡ã§æ°ããã·ã³ã¯ããã€ã¶ãŒãäœæãã3çªç®ã§äœ¿çšããå¿
èŠãããããšãã³ã³ããã¹ãã«äŒãã17çªç®ã§å¿
èŠãªãã¹ãŠã®æäœãå®äºãããŸã§åŸ
æ©ããŸãã
Statesããããè¡ãããšã
èš±å¯ããŠãããšããäºå®ã¯éåžžã«ã¯ãŒã«ã§ãããå Žåã«ãã£ãŠã¯ãã¢ããªã±ãŒã·ã§ã³ãäœæ¥ãå®äºããã®ãåŸ
ã€å¿
èŠããªããªããŸãã
ã€ãŒãžãŒã¢ãã¯
ããã«äœ¿çšã§ãããã«ãã¹ã¬ããåãå®å
šã«ãµããŒãããŠããŸãã ããã«ãèšé²äžã«
makeThreadSafe(mock, false)
ã䜿çšããŠ
ç¡å¹ã«ããå¿
èŠã«å¿ããŠã
checkIsUsedInOneThread(mock, true)
ãšèšã£ãŠ
ã1ã€ã®ã¹ã¬ããããã¢ãã¯ã䜿çšãããããšã確èªã checkIsUsedInOneThread(mock, true)
æš¡æ¬ã¯ã©ã¹
ãã¹ãã§ã¢ãã¯ã«çœ®ãæããå¿
èŠãããã¢ãžã¥ãŒã«ã®éçºè
ã¯ãã€ã³ã¿ãŒãã§ã€ã¹ãäœæããããšãæ°ã«ãããç¹å®ã®ã¯ã©ã¹ããæã£ãŠããªãããšãå€æããå ŽåããããŸãã 幞ããªããšã«ããã®ã¯ã©ã¹ãfinalã§ãªããfinalã¡ãœãããæããªãå Žåãã¢ãã¯ãäœæã§ããŸãã
ãžã§ã¢ãã¯
1 2 3 4 5 6 7 8 | private Mockery context = new JUnit4Mockery() {{ this.setImposteriser(ClassImposteriser.INSTANCE); }}; @Test public void testClassMockingWithJMock() { |
å¿
èŠãªã®ã¯ãã³ã³ããã¹ãã§
setImposteriser
ã¡ãœãããåŒã³åºãããš
setImposteriser
ã§ãã äŸã§ã¯ãããã¯2è¡ç®ã§çºçããŸãã
ã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 | import static org.easymock.classextension.EasyMock.*; @Test public void testClassMockingWithEasyMock() { |
å¥ã®ã¯ã©ã¹ã®ã¡ãœããã䜿çšããã ãã§ååã§ãããããã¯å¥ã®Mavenã¢ãŒãã£ãã¡ã¯ãã«ãããŸãã ãã ããã¯ã©ã¹æ¡åŒµã䜿çšãããšãã¯ã©ã¹ãšã€ã³ã¿ãŒãã§ã€ã¹ã®äž¡æ¹ã§å®å
šã«mokaãäœæã§ããŸãã
éšåçãªã¢ãã¯
ããã§ãæã
ãã¯ã©ã¹ã¡ãœããã®äžéšã§ã®ã¿ã¢ãã¯ãäœæããæ®ãã®éšåã«ã¯æã觊ããªãããã«ããå¿
èŠãããå ŽåããããŸãã
JMockïŒ ãã®ãããªæ©äŒã¯ãããŸãã
ã€ãŒãžãŒã¢ãã¯
1 2 3 4 5 6 7 | @Test public void testPartialMockingWithEasyMock() { |
ãã¹ãŠãã·ã³ãã«ã§æ確ã§ãã ãŸããã©ã®ãã©ã¡ãŒã¿ãŒãã©ã®ã³ã³ã¹ãã©ã¯ã¿ãŒã«æž¡ãããæå®ããããšãã§ããŸãã ã¢ãã¯ãããŠããªãã¡ãœããã¯ãã¹ãŠããã®ã¯ã©ã¹ã«å§ä»»ãããŸãã
ãããã«
ããã§äŸãšæ¯èŒãå®äºããŸããã ãããŸã§èªãã ãã¹ãŠã®äººããäž¡æ¹ã®ã©ã€ãã©ãªã®äœ¿çšæ¹æ³ãç¥ã£ãŠãããå°æ¥ãç¿åŸããã¹ãã«ã䜿çšããŠãå€ãã®ééãããèªåèªèº«ãæãããšãé¡ã£ãŠããŸãã
ããããèªè
ã¯ãããããžã§ã¯ãã§äœã䜿ãã¹ãã§ããïŒããšãã質åã«å¯Ÿããç§ããã®åçãåŸ
ã£ãŠããã®ã§ãããã JMockãŸãã¯EasyMockïŒã ããã§ã®çãã¯éåžžã«ã·ã³ãã«ã§æ確ã§ããããããžã§ã¯ãã®èŠä»¶ã«äŸåããŸãã ãããã®åã©ã€ãã©ãªã¯ããŒã«ã§ãããåã©ã€ãã©ãªã䜿çšã§ããå¿
èŠããããç¹å®ã®ã¿ã¹ã¯ã«äœ¿çšããã©ã€ãã©ãªãéžæããå¿
èŠããããŸãã
ããã ãã§ã èå³æ·±ã質åãã³ã¡ã³ããåŸ
ã£ãŠããŸãïŒ