Search code examples
flutterdartmockitoflutter-testdart-null-safety

After migrating flutter code to null-safety, mock objects not accepting `any`


After the release of Flutter 2, I've migrated my code to sdk: '>=2.12.0 <3.0.0' and all codes are "sound null safety" now. But I encountered errors in unit tests with mockito 5.0.0

e.g:

when(mockClient.login(any)).thenThrow(GrpcError.unavailable());

was ok earlier, but now, the compiler shows an error under any, indicating: The argument type 'Null' can't be assigned to the parameter type 'LoginRequest'

I read this link from mockito repo but I hope there is an easier way to write tests for methods with "not nullable" arguments like before.


Solution

  • See the solution here. You can use the mocktail package which makes it way easier.

    With mocktail your code would become

    when(() => mockClient.login(any())).thenThrow(GrpcError.unavailable());