SoftwareEngineering/Java/UnitTest
Mockito†
1. Let's verify some behaviour!†
2. How about some stubbing?†
3. Argument matchers†
4. Verifying exact number of invocations / at least once / never†
package org.codereign.mockito.tutorial.tutorial04;
import static org.mockito.Mockito.*;
import java.util.List;
import org.junit.Test;
/**
* 呼び出し回数を検証します。
*/
public class Tutorial04Test {
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test() {
// ---------------------------------------------------------
// Arrange
// ---------------------------------------------------------
List mockedList = mock(List.class);
// ---------------------------------------------------------
// Act
// ---------------------------------------------------------
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
// ---------------------------------------------------------
// Assert
// ....呼び出し回数が想定していた回数と一致しているか検証する
// ---------------------------------------------------------
verify(mockedList, times(0)).add("never happened");
verify(mockedList, times(1)).add("once");
verify(mockedList, times(2)).add("twice");
verify(mockedList, times(3)).add("three times");
// ---------------------------------------------------------
// ....呼び出し回数が想定していた回数以下であるか検証する
// ---------------------------------------------------------
verify(mockedList, atMost(0)).add("never happened");
verify(mockedList, atMost(1)).add("never happened");
verify(mockedList, atMost(1)).add("once");
verify(mockedList, atMost(2)).add("never happened");
verify(mockedList, atMost(2)).add("once");
verify(mockedList, atMost(2)).add("twice");
verify(mockedList, atMost(3)).add("never happened");
verify(mockedList, atMost(3)).add("once");
verify(mockedList, atMost(3)).add("twice");
verify(mockedList, atMost(3)).add("three times");
// ---------------------------------------------------------
// ....呼び出し回数が想定していた回数以上であるか検証する
// ---------------------------------------------------------
verify(mockedList, atLeast(0)).add("never happened");
verify(mockedList, atLeast(0)).add("once");
verify(mockedList, atLeast(0)).add("twice");
verify(mockedList, atLeast(0)).add("three times");
verify(mockedList, atLeast(1)).add("once");
verify(mockedList, atLeast(1)).add("twice");
verify(mockedList, atLeast(1)).add("three times");
verify(mockedList, atLeast(2)).add("twice");
verify(mockedList, atLeast(2)).add("three times");
verify(mockedList, atLeast(3)).add("three times");
}
}
5. Stubbing void methods with exceptions†
6. Verification in order†
7. Making sure interaction(s) never happened on mock†
8. Finding redundant invocations†
9. Shorthand for mocks creation - @Mock annotation†
10. Stubbing consecutive calls (iterator-style stubbing)†
11. Stubbing with callbacks†
12. doReturn()|doThrow()|doAnswer()|doNothing()|doCallRealMethod() family of methods†
13. Spying on real objects†
14. Changing default return values of unstubbed invocations (Since 1.7)†
15. Capturing arguments for further assertions (Since 1.8.0)†
16. Real partial mocks (Since 1.8.0)†
17. Resetting mocks (Since 1.8.0)†
18. Troubleshooting & validating framework usage (Since 1.8.0)†
19. Aliases for behavior driven development (Since 1.8.0)†
20. Serializable mocks (Since 1.8.1)†
21. New annotations: @Captor, @Spy, @InjectMocks (Since 1.8.3)†
22. Verification with timeout (Since 1.8.5)†
23. Automatic instantiation of @Spies, @InjectMocks and constructor injection goodness (Since 1.9.0)†
24. One-liner stubs (Since 1.9.0)†
25. Verification ignoring stubs (Since 1.9.0)†
26. Mocking details (Improved in 2.2.x)†
27. Delegate calls to real instance (Since 1.9.5)†
28. MockMaker API (Since 1.9.5)†
29. BDD style verification (Since 1.10.0)†
30. Spying or mocking abstract classes (Since 1.10.12, further enhanced in 2.7.13 and 2.7.14)†
31. Mockito mocks can be serialized / deserialized across classloaders (Since 1.10.0)†
32. Better generic support with deep stubs (Since 1.10.0)†
33. Mockito JUnit rule (Since 1.10.17)†
34. Switch on or off plugins (Since 1.10.15)†
35. Custom verification failure message (Since 2.1.0)†
36. Java 8 Lambda Matcher Support (Since 2.1.0)†
37. Java 8 Custom Answer Support (Since 2.1.0)†
38. Meta data and generic type retention (Since 2.1.0)†
39. Mocking final types, enums and final methods (Since 2.1.0)†
40. Improved productivity and cleaner tests with "stricter" Mockito (Since 2.+)†
41. Advanced public API for framework integrations (Since 2.10.+)†
42. New API for integrations: listening on verification start events (Since 2.11.+)†
43. New API for integrations: MockitoSession is usable by testing frameworks (Since 2.15.+)†
44. Deprecated org.mockito.plugins.InstantiatorProvider as it was leaking internal API. it was replaced by org.mockito.plugins.InstantiatorProvider2 (Since 2.15.4)†
45. New JUnit Jupiter (JUnit5+) extension†
46. New Mockito.lenient() and MockSettings.lenient() methods (Since 2.20.0)†
47. New API for clearing mock state in inline mocking (Since 2.25.0)†
48. New API for mocking static methods (Since 3.4.0)†
49. New API for mocking object construction (Since 3.5.0)†