以åã®æ¥è¨ã§EasyMockããç´¹ä»ããã®ã§ãããç¾å¨ã®ç¾å ´ã§ã¯Mockitoã使ã£ã¦ãã¾ãã
ããã«ãã¦ãã¢ãã¯ãããï¼ã¢ãããï¼ã¢ãã¼ãï¼ã©ãèªããã ããâ¦
ãã´ã¯æåãªã«ã¯ãã«ãã¢ãã¼ããã§ã¯ããããåã¯ãã³ããè¦æãªã®ã§ä½ã飲ã¾ãªããã©ã
Mockitoã¯æ¯è¼çæ°ããã¢ãã¯ã¦ã¼ãã£ãªãã£ã§ãã
ä½ãé©æ°çãªæ©è½ãåãã¦ãããã¨ããã¨ããã§ããªãã®ã§ããã使ãåæãé常ã«è¯ãã®ãç¹å¾´ã
ãµã³ãã«ã³ã¼ãã¯ãããªæãã
public class VeryHugeBusinessLogic { void doSomethingHardProcessing() { ... } } public class VeryHugeService { @Autowired VeryHugeBusinessLogic logic; public void execute() { logic.doSomethingHardProcessing(); } } /** * VeryHugeServiceã®ãã¹ã */ public class VeryHugeServiceTest { public void testExecute() { VeryHugeBusinessLogic mockLogic = Mockito.mock(VeryHugeBusinessLogic.class); VeryHugeService service = new VeryHugeService(); service.logic = mockLogic; service.execute(); // doSomethingHardProcessingãå¼ã°ãããã¨ãç¢ºèª Mockito.verify(mockLogic).doSomethingHardProcessing(); } }
spyæé«ï¼
ãã¦ãããã¾ã§ã®èª¬æãèªãã§ãEasyMockã¨å¤§ãã¦å¤ãããªãããï¼ãã¨æã£ãããã®è²´æ¹ã
éããã§ããMockitoã«ã¯1ã¤ã ããEasyMockã«ã¯ããã§ããªãç´ æ´ãããæ©è½ãåå¨ããã®ã§ãã
ããã¯ä½ãã¨è¨ãã¨...Mockito#spyã¡ã½ããã
ã»ã¨ãã©ããã ãã®ããã«ãåã¯Mockitoã¸ä¹ãæãã決å¿ããã¾ããã
ä¾ãã°ã次ã®ãããªã¯ã©ã¹ãèãã¦ã¿ã¾ãã
import java.sql.*; public class FooBusinessLogic { public void execute() { if (isAnyCondition()) { doSomething(); } else { doOtherthing(); } } protected boolean isAnyCondition() { // DBã«æ¥ç¶ãã«è¡ãè¤éãªå¦ç } protected void doSomething() { // ãã¯ãDBã«æ¥ç¶ãã«è¡ãè¤éãªå¦ç } protected void doOtherthing() { // ããã¤ãDBã«æ¥ç¶ãã«è¡ãè¤éãªå¦ç } }
ãããªã¯ã©ã¹ããã£ãã¨ããå¾æ¥ãªãã°JUnitã§
import org.junit.*; public class FooBusinessLogicTest { /** * isAnyConditionãtrueãè¿ããdoSomethingãå¼ã°ãããã¨ãæ¤è¨¼ããã */ @Test public void testExecuteDoSomethingInvoked() { // doSomethingãå®è¡ããããå¦ãã®å¤å®ãã©ã° final boolean[] doSomethingInvoked = { false }; FooBusinessLogic logic = new FooBusinessLogic() { @Override protected boolean isAnyCondition() { return true; } @Override protected void doSomething() { doSomethingInvoked[0] = true; } @Override protected void doOtherthing() { Assert.fail(); } }; logic.execute(); // doSomethingãå¼ã°ãããã¨ãç¢ºèª Assert.assertTrue(doSomethingInvoked[0]); } }
ã¨ããã£ã¦ããããã§ãã
ãã©ã§ãã!! Javaã®ãã¡ãã¡ã©ã³ãã³ã°ä¸ä½ã®ç¡åã¯ã©ã¹ã
ãã¨ãdoSomethingInvoked[0]ã®ããããç¾ãããªãã
ãããªè¦è¦ããã³ã¼ãããMockkitoã使ãã¨ãããªãã¬ã¤ã«å¤èº«ããããã§ãã
import static org.mockito.Mockito.*; public class FooBusinessLogicTest { /** * isAnyConditionãtrueãè¿ããdoSomethingãå¼ã°ãããã¨ãæ¤è¨¼ããã */ @Test public void testExecuteDoSomethingInvoked() { FooBusinessLogic logic = spy(new FooBusinessLogic()); // isAnyConditionã®ä¸èº«ãå®è¡ãããå¼·å¶çã«trueãè¿ã doReturn(true).when(logic).isAnyCondition(); // doSomethingãå¼ã°ãã¦ãä½ãããªã doNothing().when(logic).doSomething(); // doOtherthingãå¼ã°ãããã¢ãµã¼ã·ã§ã³ã¨ã©ã¼ doThrow(new org.junit.AssertionFailedError()).when(logic).doOtherthing(); logic.execute(); // doSomethingãä¸åº¦ã ãå¼ã°ãããã¨ãç¢ºèª verify(logic, times(1)).doSomething(); } }
Mockito#spyã®ã¹ã´ã¤ã¨ããã¯ãå
·è±¡ã¯ã©ã¹ã®ã¡ã½ããã®ãã¡ã
æåãå
¥ãæ¿ãããã¡ã½ããã ããå
¥ãæ¿ããããã¨ãããã¨ã
ä¾ãã°EasyMockã ã¨ã
FooBusinessLogic mockLogic = EasyMock.createMock(FooBusinessLogic.class); // isAnyConditionãtrueãè¿ãããã«ä»åãã EasyMock.expect(mockLogic.isAnyCondition()).andReturn(true); // doSomethingãå¼ã³åºããã¦ãä½ãããªãããã«ä»åãã mockLogic.doSomething(); EasyMock.expectLastCall(); // ãã¹ãå®è¡â¦ï¼ mockLogic.execute(); EasyMock.replay();
ãªã©ã¨æ¸ãã°è¡ãããã«è¦ãã¾ãããããã¯ãã¡ã§ãã
ãªããªãmockLogicã¯å®å
¨ã«å
ã®æ¯ãèãã失ã£ã¦ãããä¸èº«ã¯ç©ºã£ã½ã ããã
mockLogic.executeãå¼ã³åºãã¦ãEasyMockã®ã¨ã©ã¼ã«ãªã£ã¦çµããã ããªã®ã§ãã
ä»æ¥ããåãMockitoä¿¡è
ããä¹ãæãããã ä»ããã«ãNOW!
追è¨
PowerMockã¨ããã®ãä½µç¨ããã°ãEasyMockã§spyç¸å½ã®ãã¨ãåºæ¥ãããã§ãã
ãããããã
public static <T> T createPartialMock(Class<T> type, Class<? super T> where, String... methodNames)
ããï¼ã¡ã½ããåãæååã§æ¸¡ãå¿
è¦ãããã®ãï¼
ããããEclipseã§ãªãã¡ã¯ã¿ãªã³ã°ããã¨ãã«ãèªåçã«ã¯è¿½å¾ã§ããªããããã
ã¨ããããã§ã¤ãã¤ããªæãã§ãã