How to mock each case with Mockito 1x https://qiita.com/taka_22/items/0c13aacf6ffbc2c77970 Continued.
Mockito 1.10.19 PowerMock 1.7.3 JUnit 4.11 Java 1.8.0_151
Handles cases where dependent objects are created with new, static methods, and private methods. Based on the previous pattern, we classified it into the following 12 patterns.
No | pattern | Return type of mock target method | Mock range | Mock operation |
---|---|---|---|---|
7 | The class you want to mock is generated with new | Other than void | The entire | Returns the specified return value |
8 | The class you want to mock is generated with new | Other than void | The entire | Throw an exception |
9 | The class you want to mock is generated with new | void | The entire | Throw an exception |
10 | Mock target is static method | Other than void | The entire | Returns the specified return value |
11 | Mock target is static method | Other than void | The entire | Throw an exception |
12 | Mock target is static method | void | The entire | Throw an exception |
13 | Mock target is private method | Other than void | The entire | Returns the specified return value |
14 | Mock target is private method | Other than void | The entire | Throw an exception |
15 | Mock target is private method | void | The entire | Throw an exception |
16 | Mock target is private method | Other than void | Only some methods | Returns the specified return value |
17 | Mock target is private method | Other than void | Only some methods | Throw an exception |
18 | Mock target is private method | void | Only some methods | Throw an exception |
DateUtil.java import java.text.SimpleDateFormat; import java.util.Date; public class DateUtil { private Boolean flag; public static String getCurrentDate() { Date d = new Date(); return new SimpleDateFormat("yyyy/MM/dd").format(d); } public static void setCurrentDate(long currentTime) { Date d = new Date(); d.setTime(currentTime); } public String getYYYYMMDDStringWrapper(Date date) throws Exception { return getYYYYMMDDString(date); } private String getYYYYMMDDString(Date date) throws Exception { return new SimpleDateFormat("yyyyMMdd").format(date); } private void setFlag(Boolean flag) throws Exception { this.flag = flag; } }
import static org.powermock.api.mockito.PowerMockito.doThrow; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.spy; import static org.powermock.api.mockito.PowerMockito.whenNew;
@RunWith(PowerMockRunner.class) @PrepareForTest({ DateUtil.class }) public class PowerMockTest { ...
No.7 Makes whenNew return a mock of the object created by new.
/**
// Recording phase Date dateMocked = mock(Date.class); whenNew(Date.class).withNoArguments().thenReturn(dateMocked); when(dateMocked.getTime()).thenReturn(expected.getTime());
// Replay phase String strDate = DateUtil.getCurrentDate();
// Verification phase assertThat(strDate, is("2015/05/05")); }
No.8 /**
// Replay & Verification Phase try { DateUtil.getCurrentDate(); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("aaa")); } }
No.9 /**
// Replay & Verification Phase try { DateUtil.setCurrentDate(123456789L); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("bbb")); } }
No.10 Mock static methods with mockStatic.
/**
// Replay & Verification Phase assertThat(String.valueOf(true), is("false")); assertThat(String.valueOf(false), is("false")); }
No.11 /**
// Replay & Verification Phase try { String.valueOf(true); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("aaa")); } assertNull(String.valueOf(false)); }
No.12 /**
// Replay & Verification Phase try { DateUtil.setCurrentDate(12345L); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("bbb")); } }
No.13 Since it mock the whole, the mocked method is called directly by reflection.
/**
// Replay phase Method getYYYYMMDDStringMethod = DateUtil.class.getDeclaredMethod("getYYYYMMDDString", Date.class); getYYYYMMDDStringMethod.setAccessible(true); String actual = (String)getYYYYMMDDStringMethod.invoke(dateMocked, new Date());
// Verification phase assertThat(actual, is("12345678")); }
No.14 /**
// Replay & Verification Phase try { Method getYYYYMMDDStringMethod = DateUtil.class.getDeclaredMethod("getYYYYMMDDString", Date.class); getYYYYMMDDStringMethod.setAccessible(true); getYYYYMMDDStringMethod.invoke(dateMocked, new Date()); fail(); } catch (Exception e) { if (e instanceof InvocationTargetException) { InvocationTargetException ite = (InvocationTargetException) e; assertThat(ite.getTargetException().getMessage(), is("aaa")); } else { fail(); } } }
No.15 /**
// Replay & Verification Phase Method setFlagMethod = DateUtil.class.getDeclaredMethod("setFlag", Boolean.class); setFlagMethod.setAccessible(true); try { setFlagMethod.invoke(dateMocked, true); fail(); } catch (Exception e) { if (e instanceof InvocationTargetException) { InvocationTargetException ite = (InvocationTargetException) e; assertThat(ite.getTargetException().getMessage(), is("bbb")); } else { fail(); } } }
No.16 The only difference in how to mock is to use spy. The rest is the same as No.13-15. It doesn't mock everything, so you can test it via public methods. Of course, reflection is also possible. /**
// Replay phase String strDate = dateMocked.getYYYYMMDDStringWrapper(new Date());
// Verification phase assertThat(strDate, is("12345678")); }
No.17 /**
// Replay & Verification Phase try { dateMocked.getYYYYMMDDStringWrapper(new Date()); fail(); } catch (RuntimeException e) { assertThat(e.getMessage(), is("aaa")); } }
No.18 /**
// Replay & Verification Phase Method setFlagMethod = DateUtil.class.getDeclaredMethod("setFlag", Boolean.class); setFlagMethod.setAccessible(true); try { setFlagMethod.invoke(dateMocked, true); fail(); } catch (Exception e) { if (e instanceof InvocationTargetException) { InvocationTargetException ite = (InvocationTargetException) e; assertThat(ite.getTargetException().getMessage(), is("bbb")); } else { fail(); } } }
Mockito http://site.mockito.org/ PowerMock https://github.com/powermock/powermock
https://github.com/taka2/mockito-sample
https://qiita.com/taka_22/items/0c13aacf6ffbc2c77970
Recommended Posts