Mockito Best Practices


Instantiating mocks

  • We can use MockitoJUnitRunner or MockitoRule.
  • Prefer using MockitoRule as we can use another runner or when test class extends another base class which uses another runner.

Create Mock and Stub

  • Mockito creates dummies which returns “smart zeros” when called, i.e. empty list, zero, false or, as a last resort, null.
  • Create a stub for the relevant methods to return a specific value
    • Mockito.when(someObject.someMethod()).thenReturn(someValue)
    • we can return different values based on the number of calls to the method, or by examining the input parameters (with parameter matchers).
Mock chained calls
Spy
  • used to wrap a real object. Every call, unless specified otherwise, is delegated to the object;
  • we can mock or verify some methods.
  • Prefer use a real object instead of spy
doReturn|Answer|Throw()
Argument Matcher
  • org.mockito.AdditionalMatchers: gt, lt, and, or, not…

Verify

  • Verify the expect method(s) are called with the expected value(s) on the given mock(s)
    • Mockito.verify(someObject).someMethod()
    • only verify if calling that exact method matters.
  • The default is times(1); exactly once.
  • never(), times(n) exactly n times, atLeast(n), atMost(n)
  • Verifying the order of calls: InOrder()
    • InOrder() ensures the listed calls occur in the order specified. Other calls can occur in any order
  • Only use verifyZeroInteractions(), verifyNoMoreInteractions() when needed.
    • verifyZeroInteractions(mock) ensures the test never talked to that mock.
    • verifyNoMoreInteractions(mock) ensures that there have been no calls to the mock (of any method) that have not been verified
Verify with ArgumentCaptor

Don’t use @InjectMocks or initMocks(this)

  • creating a constructor to make the dependencies visible.

###s# Prefer type-safe matchers, expectation setters, and verifiers. - The type-safe approaches can catch type mismatch at compiler time (if we refactor code and change the type later.)

Mock Best Practices

  • Follow Arrange Act Assert (AAA) when Write Test
  • Tests functionality, not implementation
  • Prefer testing state over testing interactions
  • Don’t Replace Asserts with Verify
  • Prefer real objects over fake/mock objects
  • Prefer fake objects over mock objects
  • Don’t Overuse Mocks
Resources

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)