Mock class Null safety

Extend or mixin this class to mark the implementation as a Mock.

A mocked class implements all fields and methods with a default implementation that does not throw a NoSuchMethodError, and may be further customized at runtime to define how it may behave using when.

Example use:

// Real class.
class Cat {
  String getSound(String suffix) => 'Meow$suffix';

// Mock class.
class MockCat extends Mock implements Cat {}

void main() {
  // Create a new mocked Cat at runtime.
  final cat = MockCat();

  // When 'getSound' is called, return 'Woof'
  when(() => cat.getSound(any())).thenReturn('Woof');

  // Try making a Cat sound...
  print(cat.getSound('foo')); // Prints 'Woof'

A class which extends Mock should not have any directly implemented overridden fields or methods. These fields would not be usable as a Mock with verify or when. To implement a subset of an interface manually use Fake instead.

WARNING: Mock uses noSuchMethod, which is a form of runtime reflection, and causes sub-standard code to be generated. As such, Mock should strictly not be used in any production code, especially if used within the context of Dart for Web (dart2js, DDC) and Dart for Mobile (Flutter).




hashCode int
The hash code for this object.
runtimeType Type
A representation of the runtime type of the object.


noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed.
toString() String
A string representation of this object.


operator ==(Object other) bool
The equality operator.

Static Methods

throwOnMissingStub({void exceptionBuilder(Invocation)?}) → void
A method which can be invoked to trigger all unstubbed methods to result in exceptions.