MissingPluginException while running Unit Test in Flutter

254
test('Barcode Method Test',() async {
  const MethodChannel('google_ml_kit')
      .setMockMethodCallHandler((MethodCall methodCall) async {
    if (methodCall.method == 'vision#startBarcodeScanner') {
      return <Barcode>[];
    }
    return null;
  });
  BarcodeScannerUtil barcodeScannerUtil=BarcodeScannerUtil();
  List<Barcode> barcodeList=await barcodeScannerUtil.getBarcode(InputImage.fromFilePath(r'C:\FlutterProjects\KK\Project\Dev\assets\images\ac.png'));
  expect(barcodeList.length, 0);
});
Share:
254
MSARKrish
Author by

MSARKrish

I Interested in working on Android, Now i'm fall in love with Flutter. I have two Apps in Google Play Store. CGPA Calculator New Hand Cricket New

Updated on January 01, 2023

Comments

  • MSARKrish
    MSARKrish over 1 year

    I'm using Google ML Kit Plugin for scanning barcode and qr code from given Image, I tried to write unit test for processImage method which is provided by the Google ML Kit Plugin. While running running my test code I'm facing this error. I attached my unit test code and application code.

    Error:

    C:\FlutterSDK\Flutter2.5.2\flutter\bin\flutter.bat --no-color test --machine --start- 
    paused test\view\widgets\barcodescanner_test.dart
    Testing started at 11:20 ...
    
    package:flutter/src/services/platform_channel.dart 154:7  MethodChannel._invokeMethod
    
    MissingPluginException(No implementation found for method vision#startBarcodeScanner on 
    channel google_ml_kit)
    

    Test Code:

    test('barcode test',() async {
    BarcodeScannerUtil barcodeScannerUtil=BarcodeScannerUtil();
    List<Barcode> barcodeList=await barcodeScannerUtil.getBarcode(InputImage.fromFilePath(r'C:\FlutterProjects\KK\Project\Dev\assets\images\ac.png'));
    expect(barcodeList.length, 0);
    });
    

    My Code:

    class BarcodeScannerUtil {
      BarcodeScanner barcodeScanner = GoogleMlKit.vision.barcodeScanner();
    
      Future<List<Barcode>> getBarcode(InputImage inputImage) async {
        List<Barcode> barcodeList = await barcodeScanner.processImage(inputImage);
        return barcodeList;
      }
    
      void close() {
        barcodeScanner.close();
      }
    }