Smockito

com.bdmendes.smockito.Smockito
See theSmockito companion object
trait Smockito

The trait to mix in for using Smockito facilities in a specification. It includes the mock and spy methods for creating mocks, and the various syntax extensions on them, such as on, calls and times. Those extensions expect method references via context functions with a Mock context parameter, retrievable via the it method.

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def it[T](using mock: Mock[T]): T

Retrieves the mock in scope. This is the recommended way to refer to a mock available in context, as is the case when using methods of Mock.

Retrieves the mock in scope. This is the recommended way to refer to a mock available in context, as is the case when using methods of Mock.

Type parameters

T

the mocked type.

Value parameters

mock

the mock in scope.

Attributes

Returns

the mock in scope.

def mock[T : ClassTag]: Mock[T]

Creates a Mock instance of T.

Creates a Mock instance of T.

A Mock[T] is the Smockito compile time representation of a type mocked by Mockito, erased at runtime. As such, after you set up method stubs, you may pass a mock anywhere a T is needed.

Type parameters

T

the type to mock.

Attributes

Returns

the mock instance.

def spy[T](realInstance: T): Spy[T]

Creates a Spy instance of T.

Creates a Spy instance of T.

A Spy[T] is the Smockito compile time representation of a type spied by Mockito, erased at runtime. Keep in mind that Mockito spies are copies of real instances, so any method side effects won't affect the original object.

A Spy[T] is also a Mock[T], so you may use all methods available on Mock.

If you only need to forward a few methods to a real instance, consider using the forward method on a Mock instead for clarity and performance.

Type parameters

T

the type to spy.

Attributes

Returns

the spy instance.

Extensions

Inherited extensions

extension [T](mock: Mock[T])
inline def calledAfter[A1 <: Tuple, R1, A2 <: Tuple, R2](a: (Mock[T]) ?=> MockedMethod[A1, R1], b: (Mock[T]) ?=> MockedMethod[A2, R2]): Boolean

Whether the last invocation of method a happened after the last invocation of method b, provided both methods were called at least once. Same as calledBefore(b, a).

Whether the last invocation of method a happened after the last invocation of method b, provided both methods were called at least once. Same as calledBefore(b, a).

Value parameters

a

the method that is expected to be called after b.

b

the method that is expected to be called first.

Attributes

Returns

Whether a was called after b.

Inherited from:
MockSyntax (hidden)
inline def calledBefore[A1 <: Tuple, R1, A2 <: Tuple, R2](a: (Mock[T]) ?=> MockedMethod[A1, R1], b: (Mock[T]) ?=> MockedMethod[A2, R2]): Boolean

Whether the last invocation of method a happened before the last invocation of method b, provided both methods were called at least once. Same as calledAfter(b, a).

Whether the last invocation of method a happened before the last invocation of method b, provided both methods were called at least once. Same as calledAfter(b, a).

Value parameters

a

the method that is expected to be called first.

b

the method that is expected to be called after a.

Attributes

Returns

Whether a was called before b.

Inherited from:
MockSyntax (hidden)
inline def calls[A <: Tuple, R](method: (Mock[T]) ?=> MockedMethod[A, R]): List[Pack[A]]

Yields the captured arguments received by a stubbed method, in chronological order. If you only need to reason about the number of interactions, times is more efficient.

Yields the captured arguments received by a stubbed method, in chronological order. If you only need to reason about the number of interactions, times is more efficient.

Value parameters

method

the mocked method.

Attributes

Returns

the received arguments.

Inherited from:
MockSyntax (hidden)
inline def forward[A <: Tuple, R](method: (Mock[T]) ?=> MockedMethod[A, R], realInstance: T): Mock[T]

Sets up a stub for a method that calls the respective method of a real instance.

Sets up a stub for a method that calls the respective method of a real instance.

At a high level, this desugars to:

 mock.on(it.someMethod)(realInstance.someMethod)

If you need to forward multiple methods, consider using a Spy instead.

Value parameters

method

the mocked method.

realInstance

the real instance.

Attributes

Returns

the mocked type.

Inherited from:
MockSyntax (hidden)
inline def on[A <: Tuple, R1, R2 <: R1](method: (Mock[T]) ?=> MockedMethod[A, R1])(stub: PartialFunction[Pack[A], R2]): Mock[T]

Sets up a stub for a method, based on the received tupled arguments. This will override any previous stubs for the same method.

Sets up a stub for a method, based on the received tupled arguments. This will override any previous stubs for the same method.

Value parameters

method

the method to mock.

stub

the stub implementation, based on the received arguments.

Attributes

Returns

the mocked type.

Inherited from:
MockSyntax (hidden)
inline def onCall[A <: Tuple, R1, R2 <: R1](method: (Mock[T]) ?=> MockedMethod[A, R1])(stub: PartialFunction[Int, (Pack[A]) => R2]): Mock[T]

Sets up a stub for a method that behaves differently based on the call number. For the version operating only on the expected set of inputs, see on.

Sets up a stub for a method that behaves differently based on the call number. For the version operating only on the expected set of inputs, see on.

Value parameters

method

the method to mock.

stub

the stub implementation, based on the call number of the respective method, starting at 1.

Attributes

Returns

the mocked type.

Inherited from:
MockSyntax (hidden)
inline def real[A <: Tuple, R](method: (Mock[T]) ?=> MockedMethod[A, R]): Mock[T]

Sets up a stub that delegates to the real implementation of this method. Useful when you want to preserve an adapter method’s behavior while stubbing a method lower in the hierarchy later with on. Otherwise, use with care.

Sets up a stub that delegates to the real implementation of this method. Useful when you want to preserve an adapter method’s behavior while stubbing a method lower in the hierarchy later with on. Otherwise, use with care.

Notice that, if the real implementation interacts with a class value that is not available in the mocking context, the stub will throw with a java.lang.NullPointerException. Similarly, if this method is abstract on the mocked type, this will throw at set up time.

Value parameters

method

the method whose real implementation shall be called.

Attributes

Returns

the mocked type.

Inherited from:
MockSyntax (hidden)
inline def times[A <: Tuple, R](method: (Mock[T]) ?=> MockedMethod[A, R]): Int

Yields the number of times a stub was called. If you need the exact arguments, see calls.

Yields the number of times a stub was called. If you need the exact arguments, see calls.

Value parameters

method

the mocked method.

Attributes

Returns

the number of calls to the stub.

Inherited from:
MockSyntax (hidden)