title: How to mock a static method in Flutter with Mockito?
body: <p>I have a file a function <code>fetchPosts()</code> which is in charge of getting new Posts from a server and store them in a local sqlite database.  </p>
  
  <p>As recommended on the sqflite <a href="https://github.com/tekartik/sqflite/blob/master/sqflite/doc/usage_recommendations.md" rel="noreferrer">doc</a>, I store a single ref to my database.</p>
  
  <p>Here is the content of my <code>database.dart</code> file:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:async';
  import 'package:path/path.dart';
  import 'package:sqflite/sqflite.dart';
  
  class DBProvider {
    DBProvider._();
    static final DBProvider db = DBProvider._();
  
    static Database _database;
  
    static Future&lt;Database&gt; get database async {
      if (_database != null) return _database;
      // if _database is null, we instantiate it
      _database = await _initDB();
      return _database;
    }
  
    static Future&lt;Database&gt; _initDB() async {
      final dbPath = await getDatabasesPath();
      String path = join(dbPath, 'demo.db');
  
      return await openDatabase(path, version: 1, onCreate: _onCreate);
    }
  
    static Future&lt;String&gt; insert(String table, Map&lt;String, dynamic&gt; values) async { /* insert the record*/ }
  
    // Other functions like update, delete etc.
  }
  
  </code></pre>
  
  <p>Then I use it as such in my <code>fetchPosts.dart</code> file </p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:convert';
  import 'package:http/http.dart' as http;
  import '../services/database.dart';
  
  const url = 'https://myapp.herokuapp.com';
  
  Future&lt;void&gt; fetchPosts() {
    final client = http.Client();
    return fetchPostsUsingClient(client);
  }
  
  Future&lt;void&gt; fetchPostsUsingClient(http.Client client) async {
    final res = await client.get(url);
    final posts await Post.fromJson(json.decode(response.body));
  
    for (var i = 0; i &lt; posts.length; i++) {
      await DBProvider.insert('posts', posts[i]);
    }
  }
  </code></pre>
  
  <p>In my test, how can I <code>verify</code> that <code>DBProvider.insert()</code> has been called?</p>
  
  <p><code>fetchPosts_test.dart</code></p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:test/test.dart';
  import 'package:http/http.dart' as http;
  import 'package:mockito/mockito.dart';
  import 'package:../services/fetchPosts.dart';
  
  // Create a MockClient using the Mock class provided by the Mockito package.
  // Create new instances of this class in each test.
  class MockClient extends Mock implements http.Client {}
  
  void main() {
    group('fetchPosts', () {
      test('update local db', () async {
        final client = MockClient();
  
        // Use Mockito to return a successful response when it calls the provided http.Client.
        when(client.get()).thenAnswer((_) async =&gt; http.Response('{"title": "Test"}', 200));
  
        await fetchPostsWithClient(client);
  
        verify(/* DBProvider.insert has been called ?*/);
      });
    });
  }
  </code></pre>