Skip to content

[mockito] Fake a subclass for sealed return types when stubbing - #5264

Open
AbdeldjalilChougui wants to merge 1 commit into
dart-lang:masterfrom
AbdeldjalilChougui:mockito-sealed-dummy
Open

AbdeldjalilChougui wants to merge 1 commit into
dart-lang:masterfrom
AbdeldjalilChougui:mockito-sealed-dummy

Conversation

@AbdeldjalilChougui

Copy link
Copy Markdown
Contributor

Part of #4462. This implements the idea @yanok described there (and the TODO in _dummyValueImplementing).

A sealed class can't be faked, so for a method like Pet getPet() the generated mock falls back to dummyValue<Pet>() at run time, and stubbing throws MissingDummyValueError unless the test calls provideDummy.

With this change, when the value is the returnValue passed to noSuchMethod (which is discarded once the stub or verification is recorded), the generator looks for a subclass of the sealed class that can be faked and emits a fake of that:

returnValue: _FakeCat_0(this, Invocation.method(#getPet, [id])),

A subclass is used if it is public, has no type parameters, and is not base, final or sealed. Sealed subclasses are searched recursively. If there is none, the output is the same as before.

returnValueForMissingStub is not changed, following the comment in the issue: a nice mock should not silently pick one subclass as its default. So this fixes the @GenerateMocks case; nice mocks still need provideDummy for a sealed return type, which is why this is "part of" and not "fixes".

To make the subclass importable, _TypeVisitor now also collects the direct subtypes of a sealed class it visits.

Testing:

  • New builder tests for a method, a getter, Future<Sealed>, a nested sealed class, subclasses that can't be faked, no usable subclass, and the nice-mock default.
  • I also ran the example from the issue in a small package with @GenerateMocks: when(repo.getPet(1)).thenReturn(cat) passes without provideDummy, and throws MissingDummyValueError: Pet without this change.

A sealed class cannot be faked, so a method returning one needed
provideDummy before it could be stubbed. For the returnValue passed to
noSuchMethod, which is discarded after stubbing, generate a fake of a
subclass of the sealed class instead.

The value used for missing stubs in nice mocks is unchanged, so that the
choice of a subclass never affects the code under test.

Part of dart-lang#4462.
@AbdeldjalilChougui
AbdeldjalilChougui marked this pull request as ready for review November 13, 2026 14:05
@davidmorgan

Copy link
Copy Markdown
Contributor

Thanks. It will take me a while to get to this as I am travelling then will be on vacation for a week; I'll get to it when back.

@davidmorgan davidmorgan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Using the subclass fake only for returnValue is the right line to draw: Mock.noSuchMethod returns it only while when, verify or untilCalled is in progress, so the subclass chosen never reaches code under test.

I tried a few inputs beyond the PR's tests. For a return type of Result<int> with subclasses Ok<T> extends Result<T>, Str extends Result<String> and Err extends Result<Never>, it skips the generic and wrong-type subclasses and fakes Err. A sealed type used only as a parameter generates the same code as before. The builder tests pass.

One optional idea inline. Not checked: compiling the generated code. The builder tests only match its text.

/// Direct subtypes of a sealed class are declared in the same library.
Iterable<ClassElement> _directSubtypesOfSealed(InterfaceElement element) {
if (element is! ClassElement || !element.isSealed) return const [];
return element.library.classes.where(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: library.classes leaves out enums, so a sealed class whose only implementor is an enum, sealed class Bar {} with enum E implements Bar { a }, still falls back to dummyValue<Bar>() at run time. The first enum value would work as the stubbing value, as _dummyValueImplementing already does for enum types. Fine to leave for a follow-up.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants