@@ -207,6 +207,12 @@ Also, you can also check out the example configuration in the Mockito repository
207
207
208
208
## How do I mock a ` sealed ` class?
209
209
210
+ ` sealed ` clases cannot be ` extend ` ed nor ` implement ` ed (outside of their Dart
211
+ library) and therefore cannot be mocked.
212
+
213
+
214
+ ## How do I mock a class that requires instances of a ` sealed ` class?
215
+
210
216
Suppose that you have code such as:
211
217
212
218
``` dart
@@ -223,20 +229,21 @@ class Foo {
223
229
}
224
230
```
225
231
226
- and now you want to mock ` Foo ` . The generated implementation for ` MockFoo `
227
- needs to return * something* if ` someMethod ` is called. It can't return ` null `
228
- since its return type is non-nullable. It can't construct a ` Bar ` on its own
229
- without understanding the semantics of ` Bar ` 's constructors. That is, which
230
- ` Bar ` constructor should be called and with what arguments? To avoid this,
231
- Mockito instead generates its own, fake implementation of ` Bar ` that it does
232
- know how to construct, something like:
232
+ and now you want to mock ` Foo ` . A mock implementation of ` Foo ` , generated by
233
+ ` @GenerateNiceMocks([MockSpec<Foo>()]) ` , needs to return * something* if
234
+ ` someMethod ` is called. It can't return ` null ` since its return type is
235
+ non-nullable. It can't construct a ` Bar ` on its own without understanding the
236
+ semantics of ` Bar ` 's constructors. That is, which ` Bar ` constructor should be
237
+ called and with what arguments? To avoid this, Mockito instead generates its
238
+ own, fake implementation of ` Bar ` that it does know how to construct, something
239
+ `like:
233
240
234
241
``` dart
235
242
class _FakeBar extends Fake implements Bar {}
236
243
```
237
244
238
- And then the generated implementation of ` MockFoo ` can have its ` someMethod `
239
- override return a ` _FakeBar ` instance.
245
+ And then ` MockFoo ` can have its ` someMethod ` override return a ` _FakeBar `
246
+ instance.
240
247
241
248
However, if ` Bar ` is ` sealed ` (or is marked with ` base ` or ` final ` ), then it
242
249
cannot be ` implemented ` in generated code. Therefore Mockito can't generate a
0 commit comments