Skip to content

Commit 5d23067

Browse files
committed
Mockito codegen: use fallbackGenerator when present to create a default 'returnValueForMissingStub'.
When running in sound mode, this allows overriding the default missingStub implementation that might fail at runtime (generics for ex.). PiperOrigin-RevId: 472925798
1 parent 04b74f1 commit 5d23067

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

lib/src/builder.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,9 +1350,15 @@ class _MockClassInfo {
13501350
returnValueForMissingStub =
13511351
_futureReference(refer('void')).property('value').call([]);
13521352
} else if (mockTarget.onMissingStub == OnMissingStub.returnDefault) {
1353-
// Return a legal default value if no stub is found which matches a real
1354-
// call.
1355-
returnValueForMissingStub = _dummyValue(returnType, invocation);
1353+
if (fallbackGenerator != null) {
1354+
// Re-use the fallback for missing stub.
1355+
returnValueForMissingStub =
1356+
_fallbackGeneratorCode(method, fallbackGenerator);
1357+
} else {
1358+
// Return a legal default value if no stub is found which matches a real
1359+
// call.
1360+
returnValueForMissingStub = _dummyValue(returnType, invocation);
1361+
}
13561362
}
13571363
final namedArgs = {
13581364
if (fallbackGenerator != null)
@@ -1819,7 +1825,9 @@ class _MockClassInfo {
18191825
else if (typeSystem._returnTypeIsNonNullable(getter))
18201826
'returnValue': _dummyValue(returnType, invocation),
18211827
if (mockTarget.onMissingStub == OnMissingStub.returnDefault)
1822-
'returnValueForMissingStub': _dummyValue(returnType, invocation),
1828+
'returnValueForMissingStub': (fallbackGenerator != null
1829+
? _fallbackGeneratorCode(getter, fallbackGenerator)
1830+
: _dummyValue(returnType, invocation)),
18231831
};
18241832
var superNoSuchMethod =
18251833
refer('super').property('noSuchMethod').call([invocation], namedArgs);

test/builder/custom_mocks_test.dart

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,39 @@ void main() {
935935
expect(mocksContent, contains('returnValue: _i3.mShim<T>(a: a),'));
936936
});
937937

938+
test(
939+
'generates mock classes including a fallback generator and '
940+
'OnMissingStub.returnDefault', () async {
941+
var mocksContent = await buildWithNonNullable({
942+
...annotationsAsset,
943+
'foo|lib/foo.dart': dedent(r'''
944+
abstract class Foo<T> {
945+
T get f;
946+
}
947+
'''),
948+
'foo|test/foo_test.dart': '''
949+
import 'package:foo/foo.dart';
950+
import 'package:mockito/annotations.dart';
951+
952+
T fShim<T>() {
953+
throw 'unknown';
954+
}
955+
956+
@GenerateMocks(
957+
[],
958+
customMocks: [
959+
MockSpec<Foo>(
960+
fallbackGenerators: {#f: fShim},
961+
onMissingStub: OnMissingStub.returnDefault),
962+
],
963+
)
964+
void main() {}
965+
'''
966+
});
967+
expect(mocksContent, contains('returnValue: _i3.fShim(),'));
968+
expect(mocksContent, contains('returnValueForMissingStub: _i3.fShim(),'));
969+
});
970+
938971
test(
939972
'throws when GenerateMocks is given a class with a type parameter with a '
940973
'private bound', () async {
@@ -1265,7 +1298,36 @@ void main() {
12651298
'''
12661299
});
12671300
expect(mocksContent, isNot(contains('throwOnMissingStub')));
1268-
expect(mocksContent, contains('returnValueForMissingStub:'));
1301+
expect(mocksContent, contains('returnValue: 0'));
1302+
expect(mocksContent, contains('returnValueForMissingStub: 0'));
1303+
});
1304+
1305+
test(
1306+
'generates a mock class which uses the new behavior of returning '
1307+
'a valid value for missing stubs, if GenerateNiceMocks and '
1308+
'fallbackGenerators were used', () async {
1309+
var mocksContent = await buildWithNonNullable({
1310+
...annotationsAsset,
1311+
'foo|lib/foo.dart': dedent(r'''
1312+
class Foo<T> {
1313+
int m();
1314+
}
1315+
'''),
1316+
'foo|test/foo_test.dart': '''
1317+
import 'package:foo/foo.dart';
1318+
import 'package:mockito/annotations.dart';
1319+
1320+
int mShim() {
1321+
return 1;
1322+
}
1323+
1324+
@GenerateNiceMocks([MockSpec<Foo>(fallbackGenerators: {#m: mShim})])
1325+
void main() {}
1326+
'''
1327+
});
1328+
expect(mocksContent, isNot(contains('throwOnMissingStub')));
1329+
expect(mocksContent, contains('returnValue: _i3.mShim(),'));
1330+
expect(mocksContent, contains('returnValueForMissingStub: _i3.mShim(),'));
12691331
});
12701332

12711333
test('mixed GenerateMocks and GenerateNiceMocks annotations could be used',

0 commit comments

Comments
 (0)