Skip to content

Commit d7a8334

Browse files
srawlinscopybara-github
authored andcommitted
mockito: stop using deprecated analyzer APIs.
Also remove some unused elements. PiperOrigin-RevId: 673093545
1 parent eb4d1da commit d7a8334

File tree

7 files changed

+19
-121
lines changed

7 files changed

+19
-121
lines changed

FAQ.md

Lines changed: 5 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Frequently asked questions
22

3-
## How do I mock a static method, constructor, or top-level function?
3+
#### How do I mock a static method, constructor, or top-level function?
44

55
Mockito provides its stubbing and verification features by overriding class
66
instance methods. Since there is no mechanism for overriding static methods,
@@ -58,12 +58,12 @@ for production and a [MemoryFileSystem] for tests), and use its wrapper methods
5858
[io package]: https://pub.dev/packages/io
5959
[ProcessManager]: https://pub.dev/documentation/io/latest/io/ProcessManager-class.html
6060

61-
## How do I mock an extension method?
61+
#### How do I mock an extension method?
6262

6363
If there is no way to override some kind of function, then mockito cannot mock
6464
it. See the above answer for further explanation, and alternatives.
6565

66-
## Why can a method call not be verified multiple times?
66+
#### Why can a method call not be verified multiple times?
6767

6868
When mockito verifies a method call (via [`verify`] or [`verifyInOrder`]), it
6969
marks the call as "verified", which excludes the call from further
@@ -100,7 +100,7 @@ expect(firstCall, equals(["birds"]));
100100
expect(secondCall, equals(["lizards"]));
101101
```
102102

103-
## How does mockito work?
103+
#### How does mockito work?
104104

105105
The basics of the `Mock` class are nothing special: It uses `noSuchMethod` to
106106
catch all method invocations, and returns the value that you have configured
@@ -169,7 +169,7 @@ it's done. It's very straightforward.
169169
[`verifyInOrder`]: https://pub.dev/documentation/mockito/latest/mockito/verifyInOrder.html
170170

171171

172-
## How can I customize where Mockito outputs its mocks?
172+
### How can I customize where Mockito outputs its mocks?
173173

174174
Mockito supports configuration of outputs by the configuration provided by the `build`
175175
package by creating (if it doesn't exist already) the `build.yaml` at the root folder
@@ -203,78 +203,3 @@ targets:
203203
```
204204

205205
Also, you can also check out the example configuration in the Mockito repository.
206-
207-
208-
## How do I mock a `sealed` class?
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-
216-
Suppose that you have code such as:
217-
218-
```dart
219-
class Bar {
220-
int value;
221-
222-
Bar(this.value);
223-
224-
Bar.zero() : value = 0;
225-
}
226-
227-
class Foo {
228-
Bar someMethod(int value) => Bar(value);
229-
}
230-
```
231-
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:
240-
241-
```dart
242-
class _FakeBar extends Fake implements Bar {}
243-
```
244-
245-
And then `MockFoo` can have its `someMethod` override return a `_FakeBar`
246-
instance.
247-
248-
However, if `Bar` is `sealed` (or is marked with `base` or `final`), then it
249-
cannot be `implemented` in generated code. Therefore Mockito can't generate a
250-
default value for a `Bar` on its own, and it needs users to specify the default
251-
value to use via `provideDummy` or `provideDummyBuilder`:
252-
253-
```dart
254-
import 'package:mockito/annotations.dart';
255-
import 'package:mockito/mockito.dart';
256-
257-
@GenerateNiceMocks([MockSpec<Foo>()])
258-
import 'foo.mocks.dart';
259-
260-
sealed class Bar {
261-
int value;
262-
263-
Bar(this.value);
264-
265-
Bar.zero() : value = 0;
266-
}
267-
268-
class Foo {
269-
Bar someMethod(int value) => Bar(value);
270-
}
271-
272-
void main() {
273-
provideDummy(Bar.zero());
274-
275-
var mockFoo = MockFoo();
276-
}
277-
```
278-
279-
Note that the value used as the "dummy" usually doesn't matter since methods on
280-
the mock typically will be stubbed, overriding the method's return value.

lib/mockito.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// ignore_for_file: combinators_ordering
16-
1715
// ignore: deprecated_member_use
1816
export 'package:test_api/fake.dart' show Fake;
1917

2018
export 'src/dummies.dart'
21-
show
22-
provideDummy,
23-
provideDummyBuilder,
24-
DummyBuilder,
25-
MissingDummyValueError;
19+
show provideDummy, provideDummyBuilder, MissingDummyValueError;
2620
export 'src/mock.dart'
2721
show
2822
Mock,

lib/src/builder.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ class _TypeVisitor extends RecursiveElementVisitor<void> {
329329
if (!alreadyVisitedElement) {
330330
type.element.typeParameters.forEach(visitTypeParameterElement);
331331

332-
final toStringMethod =
333-
type.element.lookUpMethod('toString', type.element.library);
332+
final toStringMethod = type.element.augmented
333+
.lookUpMethod(name: 'toString', library: type.element.library);
334334
if (toStringMethod != null && toStringMethod.parameters.isNotEmpty) {
335335
// In a Fake class which implements a class which overrides `toString`
336336
// with additional (optional) parameters, we must also override
@@ -594,9 +594,7 @@ class _MockTargetGatherer {
594594
var type = _determineDartType(typeToMock, entryLib.typeProvider);
595595
final mockTypeArguments = mockType?.typeArguments;
596596
if (mockTypeArguments != null) {
597-
final typeName =
598-
type.alias?.element.getDisplayString(withNullability: false) ??
599-
'type $type';
597+
final typeName = type.alias?.element.getDisplayString() ?? 'type $type';
600598
final typeArguments = type.alias?.typeArguments ?? type.typeArguments;
601599
// Check explicit type arguments for unknown types that were
602600
// turned into `dynamic` by the analyzer.
@@ -1849,7 +1847,7 @@ class _MockClassInfo {
18491847
// TODO(srawlins): It seems like this might be revivable, but Angular
18501848
// does not revive Types; we should investigate this if users request it.
18511849
final type = object.toTypeValue()!;
1852-
final typeStr = type.getDisplayString(withNullability: false);
1850+
final typeStr = type.getDisplayString();
18531851
throw _ReviveException('default value is a Type: $typeStr.');
18541852
} else {
18551853
// If [constant] is not null, a literal, or a type, then it must be an
@@ -2176,10 +2174,7 @@ class _MockClassInfo {
21762174
{for (final f in type.namedFields) f.name: _typeReference(f.type)})
21772175
..isNullable = forceNullable || typeSystem.isNullable(type));
21782176
} else {
2179-
return referImported(
2180-
type.getDisplayString(withNullability: false),
2181-
_typeImport(type.element),
2182-
);
2177+
return referImported(type.getDisplayString(), _typeImport(type.element));
21832178
}
21842179
}
21852180

lib/src/dummies.dart

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,12 @@ class MissingDummyValueError {
9090
MissingDummyValueError: $type
9191
9292
This means Mockito was not smart enough to generate a dummy value of type
93-
'$type'. Due to implementation details, Mockito sometimes needs users to
94-
provide dummy values for some types, even if they plan to explicitly stub all
95-
the called methods. Call either `provideDummy` or `provideDummyBuilder` to
96-
provide Mockito with a dummy value.
93+
'$type'. Please consider using either 'provideDummy' or 'provideDummyBuilder'
94+
functions to give Mockito a proper dummy value.
9795
98-
For more details, see the questions regarding `sealed` classes in the FAQ:
99-
100-
<https://github.com/dart-lang/mockito/blob/master/FAQ.md>
96+
Please note that due to implementation details Mockito sometimes needs users
97+
to provide dummy values for some types, even if they plan to explicitly stub
98+
all the called methods.
10199
''';
102100
}
103101

@@ -158,8 +156,7 @@ T dummyValue<T>(Object parent, Invocation invocation) {
158156
throw MissingDummyValueError(T);
159157
}
160158

161-
/// Specifies a builder to create a dummy value of type `T`.
162-
///
159+
/// Provide a builder for that could create a dummy value of type `T`.
163160
/// This could be useful for nice mocks, such that information about the
164161
/// specific invocation that caused the creation of a dummy value could be
165162
/// preserved.
@@ -168,11 +165,6 @@ void provideDummyBuilder<T>(DummyBuilder<T> dummyBuilder) =>
168165

169166
/// Provide a dummy value for `T` to be used both while adding expectations
170167
/// and as a default value for unstubbed methods, if using a nice mock.
171-
///
172-
/// For details and for example usage, see the questions regarding `sealed`
173-
/// classes in the [FAQ].
174-
///
175-
/// [FAQ]: https://github.com/dart-lang/mockito/blob/master/FAQ.md
176168
void provideDummy<T>(T dummy) =>
177169
provideDummyBuilder((parent, invocation) => dummy);
178170

lib/src/version.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
const packageVersion = '5.4.4';
1+
const packageVersion = '5.4.5-wip';

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: mockito
2-
version: 5.4.4
2+
version: 5.4.5-wip
33
description: >-
44
A mock framework inspired by Mockito with APIs for Fakes, Mocks,
55
behavior verification, and stubbing.

test/builder/custom_mocks_test.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ void main() {}
8181
'''
8282
};
8383

84-
const _constructorWithThrowOnMissingStub = '''
85-
MockFoo() {
86-
_i1.throwOnMissingStub(this);
87-
}''';
88-
8984
void main() {
9085
late InMemoryAssetWriter writer;
9186

@@ -1755,9 +1750,6 @@ void main() {
17551750
});
17561751
}
17571752

1758-
TypeMatcher<List<int>> _containsAllOf(a, [b]) => decodedMatches(
1759-
b == null ? allOf(contains(a)) : allOf(contains(a), contains(b)));
1760-
17611753
/// Expect that [testBuilder], given [assets], throws an
17621754
/// [InvalidMockitoAnnotationException] with a message containing [message].
17631755
void _expectBuilderThrows({

0 commit comments

Comments
 (0)