Title: Theory / Unimportant / CS / Programming / Java / Test / Mockito
Tags: automatedNewLineAfterCommaSpace seen_description
On every note of this deck, unless a note says otherwise:
Page: https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html
Language: java
Context: Mockito

Question: What should not be mocked
Answer: Values object
Page: https://site.mockito.org/
Answer2: Types you don't own

Instruction: mock(foo.class)
Section: 1
Returns: A mocked value of type foo

Instruction: mockedObject.method(
  arg1,
  ...,
  argn
)
Effect: Register in mockedObject that the method method is called with values (arg1, ..., argn)
Section: 1
Returns: Either the value specified by when, or otherwise default value null, 0, false,
Variables: Type mockedObject;

Instruction: ongoingStubbing.thenReturn(value_1)
Effect: When condition hold, return value_1
Section: 2
Returns: this
Variables: OngoingStubbing<returnType> ongoingStubbing

Instruction: ongoingStubbing.thenThrows(exception)
Effect: When the condition holds, exception is thrown
Section: 3
Returns: this
Variables: OngoingStubbing<returnType> ongoingStubbing

Instruction: when(
  mockedObject.method(
    matcher_1,
    ...,
    matcher_n
  )
)
Section: 3
Returns: A condition that is verified on values val1,..., valn when matcher_i accept the value val_i
Variables: MockedType mockedObject;
returnType method(
  vars
)
Typ: OngoingStubbing<returnType>

Instruction: eq(value)
Section: 3
Returns: the matcher matching elements equals to value

Instruction: argThat(foo)
Section: 3
Returns: the matcher that matches values v such that foo(v) holds

Instruction: when(
  mockedObject.method(
    value_1,
    ...,
    value_n
  )
)
Section: 3
Returns: when(  mockedObject.method(  eq(  value_1), ..., eq(  value_n)))
Variables: MockedType mockedObject;
returnType method(
  vars
)
Typ: OngoingStubbing<returnType>

Instruction: verify(
  mockedObject,
  property
).method(
  values_or_matchers
)
Effect: fails if the number of call of method on arguments satisfying values_or_matchers do not satisfy property
Section: 3

Instruction: verify(mockedObject).method(values_or_matchers)
Effect: verify(mockedObject, times(1)).method(values_or_matchers)
Section: 3

Instruction: times(i)
Section: 4
Returns: Property valid only if there are exactly i calls

Instruction: never()
Section: 4
Returns: times(0)

Instruction: atLeastOnce()
Section: 4
Returns: atLeast(1)

Instruction: atMostOnce()
Section: 4
Returns: atMost(1)

Instruction: atLeast(i)
Section: 4
Returns: Property that holds if there is at least i calls

Instruction: atMost(i)
Section: 4
Returns: Property that holds if there is at most i calls

Instruction: inOrder(
  mockingObject1,
  ...,
  mockingObject_n
)
Section: 4
6
Returns: An object whose verify function also check that the verified event occured in the order where they are verified

Instruction: verifyNoMoreInteractions(mockingObject)
Section: 4
8
Returns: The mock object fails if mockingObject is called again

Instruction: verifyZeroInteractions(mockingObject)
Section: 4
7
Returns: Verify that mockingObject was never interacted with

Instruction: doThrow/Answer/Nothing/Return/callRealMethod(value).when(mockedObject).method()
Effect: when(mockedObject.method()).thenThrow/Answer/Nothing/Return/callRealMethod(value)
Section: 5
Returns: this

Instruction: @Mock modifiers type var;
Effect: modifiers type var = mock(type.class)
Section: 9

Instruction: (@Mock type var) in test function
Effect: Use the mocked variable var from class
Section: 9

Instruction: ongoingStubbing.thenReturn(
  value_1,
  ...,
  value_n
)
Effect: ongoingStubbing.thenReturn(value_1).....thenReturn(value_n)
Section: 9
Returns: this
Variables: OngoingStubbing<returnType> ongoingStubbing

Instruction: ongoingStubbing.thenReturn/Throw(value_1).....thenReturn/Throw(value_n)
Effect: on i-th call, return/throw value_min(i,n).
Section: 9
Returns: this
Variables: OngoingStubbing<returnType> ongoingStubbing

Instruction: ongoingStubbing.thenAnswer(f)
Effect: When condition holds, apply f to the invocation
Section: 9
Returns: this
Variables: OngoingStubbing<returnType> ongoingStubbing
Abbreviation: ongoingStubbing.then(f)

Instruction: spy(object)
Section: 9
Returns: A mocking object, equal to object but where some method can be stubbed
Variables: type object

Instruction: mock(
  Foo.class,
  strategy
)
Section: 14
Returns: A mock value for class Foo where strategy determines the return value of function call
Variables: type object

Instruction: ArgumentCaptor.forClass(Foo.class)
Section: 15
Returns: An object that can store a value of type Foo
Variables: type object

Instruction: ongoingStubbing.thenCallRealMethod()
Effect: this
Section: 16
Returns: When this condition occur, call the real method instead of mocking it
Variables: OngoingStubbing<returnType> ongoingStubbing
Assuming: the method can be called on a non initialized object, do not depend on internal state, etc..

Instruction: mockObject.reset()
Section: 17
Returns: mockObject forgot all past interactions
Variables: type object
Assuming: Never used in actual test
Intuition: allow to share a mock object through container-injected mock.

Instruction: mock(
  Foo.class,
  setting
)
Section: 20
Returns: Same as mock(  Foo.class) with setting settings
Variables: MockSetting setting
Class Foo

Instruction: withSetting()
Section: 20
Returns: A default setting for mock object
Typ: MockSetting

Instruction: setting.spiedInstance(foo)
Effect: the mock object using this setting become a spy for object foo
Section: 20
Variables: MockSetting setting

Instruction: setting.defaultAnswer(strategy)
Effect: the mock object use default strategy strategy
Section: 20
Variables: MockSetting setting

Instruction: setting.serializable()
Effect: the mock object using this setting is serializabe
Section: 20
Variables: MockSetting setting

Instruction: @Spy modifiers type var = value;
Section: 21
Returns: modifiers type var = spy(value)

Instruction: @InjectMocks ?
Section: 21

Instruction: @Captor ?
Section: 21

Instruction: given?
Section: 19

Instruction: verify(
  mockObject,
  timeout(n)
).method()
Section: 19
Returns: Test passes if the method is called within n miliseconds

Instruction: verificationMode.description(description)
Page: https://javadoc.io/static/org.mockito/mockito-core/3.9.0/org/mockito/verification/VerificationMode.html
Effect: Prepend all assertion error with description
Returns: verificationMode
Variables: VerificationMode verificationMode;

Instruction: verificationMode.verify(foo)
Page: https://javadoc.io/static/org.mockito/mockito-core/3.9.0/org/mockito/verification/VerificationMode.html
Effect: Fail if foo did not occur
Variables: VerificationMode verificationMode;

Instruction: ignorestub?
Section: 25
Variables: VerificationMode verificationMode;

Instruction: Mockito.mockingDetails(someObject)
Section: 25
Returns: A representation of the mocking-related detail of this object
Typ: MockingDetails

Instruction: mockingDetails.isSpy();
Section: 25
Returns: Whether someObject is a mockito spy object
Variables: MockingDetails mockingDetails = Mockito.mockingDetails(someObject);

Instruction: mockingDetails.isMock()
Section: 25
Returns: Whether someObject is a mockito mock object
Variables: MockingDetails mockingDetails = Mockito.mockingDetails(someObject);

Instruction: mockingDetails.getTypeToMock()
Section: 25
Returns: the type being mocked
Variables: MockingDetails mockingDetails = Mockito.mockingDetails(someObject);

Instruction: mockingDetails.getDefaultAnswer()
Section: 25
Returns: the default answer rule followed by someObject
Variables: MockingDetails mockingDetails = Mockito.mockingDetails(someObject);

Instruction: mockingDetails.getInvocations()
Section: 25
Returns: List of invocations on someObject
Variables: MockingDetails mockingDetails = Mockito.mockingDetails(someObject);

Instruction: mockingDetails.getSubbings()
Section: 25
Returns: List of subbing on someObject
Variables: MockingDetails mockingDetails = Mockito.mockingDetails(someObject);