'Flutter testing: static method I need to mock inside code

I want to test a method that is responsible for a button tap (let's call it onButtonTap()), one of the first methods is a call to static method from utils file, that returns true of false, depending on set android/ios permissions (or allows user to change permissions by showing dialog that can open application settings). Let's call it checkOrRequestPermissions(). This makes everything behind that code untestable, as I don't know how to test it - I can't mock this class because:

  • It's not injected anywhere - it's inside utils file
  • It's static

So for better visualization lets go like this:

Code from file I want to test:

Future<void> onButtonTap(BuildContext context) async {
   bool isGranted = await PermissionsUtil.checkOrRequestPermissions([some_args]);
   // CODE_A - some code I want to test
}

Code inside PermissionsUtil:

class PermissionsUtil{
    static Future<bool> checkOrRequestPermissions([some_args]){
        // code for permissions
    }
}

So my questions are:

  1. Is there any way I could mock checkOrRequestPermissions() to simply return given value?
  2. How could I make this code testable?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source