Skip to content

Commit 1a0d0e7

Browse files
Ilya Yanokcopybara-github
authored andcommitted
Remove deprecated returnNullOnMissingStub and OnMissingStub.returnNull
This option results in runtime type errors and was superseeded by `OnMissingStub.returnDefault`. PiperOrigin-RevId: 576796424
1 parent 4edf86f commit 1a0d0e7

File tree

7 files changed

+15
-138
lines changed

7 files changed

+15
-138
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
* **Potentially breaking** Changed default `String` value returned by nice
88
mocks' unstubbed method to include some useful info. This could break the
99
tests that relied on getting an empty `String` from unstubbed methods.
10+
* Remove deprecated `returnNullOnMissingStub` and `OnMissingStub.returnNull`
11+
`MockSpec` options.
1012

1113
## 5.4.2
1214

NULL_SAFETY_README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,19 +159,6 @@ behavior, use `@GenerateMocks`:
159159
@GenerateMocks([Foo])
160160
```
161161

162-
#### Old "return null" missing stub behavior
163-
164-
To use the old default behavior of returning null (which doesn't make a lot of
165-
sense in the Null safety type system), for legacy code, use
166-
`returnNullOnMissingStub`:
167-
168-
```dart
169-
@GenerateMocks([], customMocks: [MockSpec<Foo>(returnNullOnMissingStub: true)])
170-
```
171-
172-
This option is soft-deprecated (no deprecation warning yet); it will be marked
173-
`@deprecated` in a future release, and removed in a later release.
174-
175162
#### Mocking members with non-nullable type variable return types
176163

177164
If a class has a member with a type variable as a return type (for example,

lib/annotations.dart

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,6 @@ class GenerateNiceMocks {
7878
/// The name of the mock class is either specified with the `as` named argument,
7979
/// or is the name of the class being mocked, prefixed with 'Mock'.
8080
///
81-
/// To use the legacy behavior of returning null for unstubbed methods, use
82-
/// `returnNullOnMissingStub: true`.
83-
///
8481
/// For example, given the generic class, `class Foo<T>`, then this
8582
/// annotation:
8683
///
@@ -99,10 +96,6 @@ class MockSpec<T> {
9996

10097
final List<Type> mixins;
10198

102-
@Deprecated('Specify "missing stub" behavior with the [onMissingStub] '
103-
'parameter.')
104-
final bool returnNullOnMissingStub;
105-
10699
final OnMissingStub? onMissingStub;
107100

108101
final Set<Symbol> unsupportedMembers;
@@ -117,12 +110,6 @@ class MockSpec<T> {
117110
///
118111
/// Specify a custom name with the [as] parameter.
119112
///
120-
/// If [onMissingStub] is specified as [OnMissingStub.returnNull],
121-
/// (or if the deprecated parameter [returnNullOnMissingStub] is `true`), then
122-
/// a real call to a mock method (or getter) will return `null` when no stub
123-
/// is found. This may result in a runtime error, if the return type of the
124-
/// method (or the getter) is non-nullable.
125-
///
126113
/// If [onMissingStub] is specified as
127114
/// [OnMissingStub.returnDefault], a real call to a mock method (or
128115
/// getter) will return a legal value when no stub is found.
@@ -149,10 +136,7 @@ class MockSpec<T> {
149136
Symbol? as,
150137
@Deprecated('Avoid adding concrete implementation to mock classes. '
151138
'Use a manual implementation of the class without `Mock`')
152-
List<Type> mixingIn = const [],
153-
@Deprecated('Specify "missing stub" behavior with the '
154-
'[onMissingStub] parameter.')
155-
this.returnNullOnMissingStub = false,
139+
List<Type> mixingIn = const [],
156140
this.unsupportedMembers = const {},
157141
this.fallbackGenerators = const {},
158142
this.onMissingStub,
@@ -166,14 +150,6 @@ enum OnMissingStub {
166150
/// An exception should be thrown.
167151
throwException,
168152

169-
/// A `null` value should be returned.
170-
///
171-
/// This is considered legacy behavior, as it may result in a runtime error,
172-
/// if the return type of the method (or the getter) is non-nullable.
173-
@Deprecated(
174-
'This is legacy behavior, it may result in runtime errors. Consider using returnDefault instead')
175-
returnNull,
176-
177153
/// A legal default value should be returned.
178154
///
179155
/// For basic known types, like `int` and `Future<String>`, a simple value is

lib/src/builder.dart

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -648,47 +648,15 @@ class _MockTargetGatherer {
648648
mixins.add(mixinInterfaceType);
649649
}
650650

651-
final returnNullOnMissingStub =
652-
mockSpec.getField('returnNullOnMissingStub')!.toBoolValue()!;
653651
final onMissingStubValue = mockSpec.getField('onMissingStub')!;
654652
final OnMissingStub onMissingStub;
655-
if (nice) {
656-
// The new @GenerateNiceMocks API. Don't allow `returnNullOnMissingStub`.
657-
if (returnNullOnMissingStub) {
658-
throw ArgumentError("'returnNullOnMissingStub' is not supported with "
659-
'@GenerateNiceMocks');
660-
}
661-
if (onMissingStubValue.isNull) {
662-
onMissingStub = OnMissingStub.returnDefault;
663-
} else {
664-
final onMissingStubIndex =
665-
onMissingStubValue.getField('index')!.toIntValue()!;
666-
onMissingStub = OnMissingStub.values[onMissingStubIndex];
667-
// ignore: deprecated_member_use_from_same_package
668-
if (onMissingStub == OnMissingStub.returnNull) {
669-
throw ArgumentError(
670-
"'OnMissingStub.returnNull' is not supported with "
671-
'@GenerateNiceMocks');
672-
}
673-
}
653+
if (onMissingStubValue.isNull) {
654+
onMissingStub =
655+
nice ? OnMissingStub.returnDefault : OnMissingStub.throwException;
674656
} else {
675-
if (onMissingStubValue.isNull) {
676-
// No value was given for `onMissingStub`. But the behavior may
677-
// be specified by `returnNullOnMissingStub`.
678-
onMissingStub = returnNullOnMissingStub
679-
// ignore: deprecated_member_use_from_same_package
680-
? OnMissingStub.returnNull
681-
: OnMissingStub.throwException;
682-
} else {
683-
// A value was given for `onMissingStub`.
684-
if (returnNullOnMissingStub) {
685-
throw ArgumentError("Cannot specify 'returnNullOnMissingStub' and a "
686-
"'onMissingStub' value at the same time.");
687-
}
688-
final onMissingStubIndex =
689-
onMissingStubValue.getField('index')!.toIntValue()!;
690-
onMissingStub = OnMissingStub.values[onMissingStubIndex];
691-
}
657+
final onMissingStubIndex =
658+
onMissingStubValue.getField('index')!.toIntValue()!;
659+
onMissingStub = OnMissingStub.values[onMissingStubIndex];
692660
}
693661
final unsupportedMembers = {
694662
for (final m in mockSpec.getField('unsupportedMembers')!.toSetValue()!)

test/builder/auto_mocks_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,12 @@ class GenerateMocks {
3434
class MockSpec<T> {
3535
final Symbol? mockName;
3636
37-
final bool returnNullOnMissingStub;
38-
3937
final Set<Symbol> unsupportedMembers;
4038
4139
final Map<Symbol, Function> fallbackGenerators;
4240
4341
const MockSpec({
4442
Symbol? as,
45-
this.returnNullOnMissingStub = false,
4643
this.unsupportedMembers = const {},
4744
this.fallbackGenerators = const {},
4845
})

test/builder/custom_mocks_test.dart

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ class MockSpec<T> {
4343
4444
final List<Type> mixins;
4545
46-
final bool returnNullOnMissingStub;
47-
4846
final OnMissingStub? onMissingStub;
4947
5048
final Set<Symbol> unsupportedMembers;
@@ -54,15 +52,14 @@ class MockSpec<T> {
5452
const MockSpec({
5553
Symbol? as,
5654
List<Type> mixingIn = const [],
57-
this.returnNullOnMissingStub = false,
5855
this.onMissingStub,
5956
this.unsupportedMembers = const {},
6057
this.fallbackGenerators = const {},
6158
}) : mockName = as,
6259
mixins = mixingIn;
6360
}
6461
65-
enum OnMissingStub { throwException, returnNull, returnDefault }
62+
enum OnMissingStub { throwException, returnDefault }
6663
'''
6764
};
6865

@@ -438,24 +435,6 @@ void main() {
438435
);
439436
});
440437

441-
test(
442-
'generates a mock class which uses the old behavior of returning null on '
443-
'missing stubs', () async {
444-
final mocksContent = await buildWithNonNullable({
445-
...annotationsAsset,
446-
'foo|lib/foo.dart': dedent(r'''
447-
class Foo<T> {}
448-
'''),
449-
'foo|test/foo_test.dart': '''
450-
import 'package:foo/foo.dart';
451-
import 'package:mockito/annotations.dart';
452-
@GenerateMocks([], customMocks: [MockSpec<Foo>(as: #MockFoo, returnNullOnMissingStub: true)])
453-
void main() {}
454-
'''
455-
});
456-
expect(mocksContent, isNot(contains('throwOnMissingStub')));
457-
});
458-
459438
test(
460439
'generates mock methods with private return types, given '
461440
'unsupportedMembers', () async {

test/end2end/generated_mocks_test.dart

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ import 'generated_mocks_test.mocks.dart';
1111
FooSub,
1212
Bar
1313
], customMocks: [
14-
MockSpec<Foo>(as: #MockFooRelaxed, returnNullOnMissingStub: true),
1514
MockSpec<Foo>(
1615
as: #MockFooWithDefaults,
1716
onMissingStub: OnMissingStub.returnDefault,
1817
),
19-
MockSpec<Bar>(as: #MockBarRelaxed, returnNullOnMissingStub: true),
2018
MockSpec<Baz>(
2119
as: #MockBazWithUnsupportedMembers,
2220
unsupportedMembers: {
@@ -28,7 +26,8 @@ import 'generated_mocks_test.mocks.dart';
2826
),
2927
MockSpec<HasPrivate>(mixingIn: [HasPrivateMixin]),
3028
])
31-
@GenerateNiceMocks([MockSpec<Foo>(as: #MockFooNice)])
29+
@GenerateNiceMocks(
30+
[MockSpec<Foo>(as: #MockFooNice), MockSpec<Bar>(as: #MockBarNice)])
3231
void main() {
3332
group('for a generated mock,', () {
3433
late MockFoo<Object> foo;
@@ -225,35 +224,6 @@ void main() {
225224
});
226225
});
227226

228-
group('for a generated mock using returnNullOnMissingStub,', () {
229-
late Foo<Object> foo;
230-
231-
setUp(() {
232-
foo = MockFooRelaxed();
233-
});
234-
235-
test('an unstubbed method returning a non-nullable type throws a TypeError',
236-
() {
237-
when(foo.namedParameter(x: 42)).thenReturn('Stubbed');
238-
expect(
239-
() => foo.namedParameter(x: 43), throwsA(TypeMatcher<TypeError>()));
240-
});
241-
242-
test('an unstubbed getter returning a non-nullable type throws a TypeError',
243-
() {
244-
expect(() => foo.getter, throwsA(TypeMatcher<TypeError>()));
245-
});
246-
247-
test('an unstubbed method returning a nullable type returns null', () {
248-
when(foo.nullableMethod(42)).thenReturn('Stubbed');
249-
expect(foo.nullableMethod(43), isNull);
250-
});
251-
252-
test('an unstubbed getter returning a nullable type returns null', () {
253-
expect(foo.nullableGetter, isNull);
254-
});
255-
});
256-
257227
group('for a generated mock using OnMissingStub.returnDefault,', () {
258228
late Foo<Object> foo;
259229

@@ -356,11 +326,9 @@ void main() {
356326
expect(foo.methodWithBarArg(bar), equals('mocked result'));
357327
});
358328

359-
test(
360-
'a generated mock which returns null on missing stubs can be used as a '
361-
'stub argument', () {
362-
final foo = MockFooRelaxed();
363-
final bar = MockBarRelaxed();
329+
test('a generated nice mock can be used as a stub argument', () {
330+
final foo = MockFoo();
331+
final bar = MockBarNice();
364332
when(foo.methodWithBarArg(bar)).thenReturn('mocked result');
365333
expect(foo.methodWithBarArg(bar), equals('mocked result'));
366334
});

0 commit comments

Comments
 (0)