T.apex

  • Docs
  • Tutorials
Docs Menu
  • BDD Style
  • Mocking
    • Mock Creation
    • Mock Behavior
    • Argument Predicates
    • Mock Verification
  • Test Data

Mock Behavior Guide

Mock Behavior

How to Establish Mock Behavior

We have three ways to establish mock method behaviors.

NameDescriptionScenario
ReturningReturn a valueWhen the mocked method simply needs to return something
ThrowingThrow an exceptionWhen the mocked method needs to throw an exception
AnsweringDelegate the call to a FuncWhen the mocked method needs more than simply returning something

By Returning

Here is how we establish a mock method behavior by returning.

T.when(mock.run(0)).thenReturn(0);
// When mock calls 'run' with 0, return 0

By Throwing

Here is how we establish a mock method behavior by throwing.

T.when(mock.run(0)).thenThrow(new T.TestException('test'));
// When mock calls 'run' with 0, throw the exception

By Answering

Here is how we establish a mock method behavior by answering.

T.when(mock.run(0)).thenAnswer(R.inc);
// When mock calls 'run' with 0, apply the answer Func to the arguments
// and return the result

Contribute on Github! Edit this section.