From 2ba86109dcac25948e3af6b39a7512c6d197ff51 Mon Sep 17 00:00:00 2001 From: GJJ2019 Date: Mon, 11 Jul 2022 12:16:26 +0530 Subject: [PATCH] adding test cases with mockito --- pubspec.yaml | 2 + test/easy_upi_payment_test.dart | 80 +++++++++++++++++++++------ test/easy_upi_payment_test.mocks.dart | 40 ++++++++++++++ 3 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 test/easy_upi_payment_test.mocks.dart diff --git a/pubspec.yaml b/pubspec.yaml index c0d9c06..f6135b8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,6 +16,8 @@ dev_dependencies: flutter_test: sdk: flutter lint: ^1.8.2 + mockito: ^5.2.0 + build_runner: ^2.1.11 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec diff --git a/test/easy_upi_payment_test.dart b/test/easy_upi_payment_test.dart index 4eb423e..bf82007 100644 --- a/test/easy_upi_payment_test.dart +++ b/test/easy_upi_payment_test.dart @@ -1,28 +1,76 @@ import 'package:easy_upi_payment/easy_upi_payment.dart'; -import 'package:easy_upi_payment/src/easy_upi_payment_method_channel.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:plugin_platform_interface/plugin_platform_interface.dart'; +import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; +import 'easy_upi_payment_test.mocks.dart'; import 'fake_data.dart'; -class MockEasyUpiPaymentPlatform with MockPlatformInterfaceMixin implements EasyUpiPaymentPlatform { - @override - Future startPayment(EasyUpiPaymentModel easyUpiPaymentModel) { - return Future.value(fakeTransactionDetailsModel); - } -} - +@GenerateMocks([EasyUpiPaymentPlatform]) void main() { - final EasyUpiPaymentPlatform initialPlatform = EasyUpiPaymentPlatform.instance; + final mockPlatform = MockEasyUpiPaymentPlatform(); - test('$MethodChannelEasyUpiPayment is the default instance', () { - expect(initialPlatform, isInstanceOf()); + test('startPayment', () async { + when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenAnswer( + (_) => Future.value(fakeTransactionDetailsModel), + ); + expect(await mockPlatform.startPayment(fakeEasyUpiPaymentModel), fakeTransactionDetailsModel); }); - test('startPayment', () async { - final MockEasyUpiPaymentPlatform fakePlatform = MockEasyUpiPaymentPlatform(); - EasyUpiPaymentPlatform.instance = fakePlatform; + test('startPayment with cancelled Exception', () async { + when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow( + EasyUpiPaymentException.fromException( + PlatformException( + code: EasyUpiPaymentExceptionType.cancelledException.toString(), + ), + ), + ); + expect( + () => mockPlatform.startPayment(fakeEasyUpiPaymentModel), + throwsA(isA()), + ); + }); + + test('startPayment with failed Exception', () async { + when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow( + EasyUpiPaymentException.fromException( + PlatformException( + code: EasyUpiPaymentExceptionType.failedException.toString(), + ), + ), + ); + expect( + () => mockPlatform.startPayment(fakeEasyUpiPaymentModel), + throwsA(isA()), + ); + }); + + test('startPayment with submitted Exception', () async { + when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow( + EasyUpiPaymentException.fromException( + PlatformException( + code: EasyUpiPaymentExceptionType.submittedException.toString(), + ), + ), + ); + expect( + () => mockPlatform.startPayment(fakeEasyUpiPaymentModel), + throwsA(isA()), + ); + }); - expect(await fakePlatform.startPayment(fakeEasyUpiPaymentModel), fakeTransactionDetailsModel); + test('startPayment with unknown Exception', () async { + when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow( + EasyUpiPaymentException.fromException( + PlatformException( + code: EasyUpiPaymentExceptionType.unknownException.toString(), + ), + ), + ); + expect( + () => mockPlatform.startPayment(fakeEasyUpiPaymentModel), + throwsA(isA()), + ); }); } diff --git a/test/easy_upi_payment_test.mocks.dart b/test/easy_upi_payment_test.mocks.dart new file mode 100644 index 0000000..9e5d112 --- /dev/null +++ b/test/easy_upi_payment_test.mocks.dart @@ -0,0 +1,40 @@ +// Mocks generated by Mockito 5.2.0 from annotations +// in easy_upi_payment/test/easy_upi_payment_test.dart. +// Do not manually edit this file. + +import 'dart:async' as _i3; + +import 'package:easy_upi_payment/src/easy_upi_payment_platform_interface.dart' + as _i2; +import 'package:easy_upi_payment/src/model/easy_upi_payment_model.dart' as _i5; +import 'package:easy_upi_payment/src/model/transaction_detail_model.dart' + as _i4; +import 'package:mockito/mockito.dart' as _i1; + +// ignore_for_file: type=lint +// ignore_for_file: avoid_redundant_argument_values +// ignore_for_file: avoid_setters_without_getters +// ignore_for_file: comment_references +// ignore_for_file: implementation_imports +// ignore_for_file: invalid_use_of_visible_for_testing_member +// ignore_for_file: prefer_const_constructors +// ignore_for_file: unnecessary_parenthesis +// ignore_for_file: camel_case_types + +/// A class which mocks [EasyUpiPaymentPlatform]. +/// +/// See the documentation for Mockito's code generation for more information. +class MockEasyUpiPaymentPlatform extends _i1.Mock + implements _i2.EasyUpiPaymentPlatform { + MockEasyUpiPaymentPlatform() { + _i1.throwOnMissingStub(this); + } + + @override + _i3.Future<_i4.TransactionDetailModel?> startPayment( + _i5.EasyUpiPaymentModel? easyUpiPaymentModel) => + (super.noSuchMethod( + Invocation.method(#startPayment, [easyUpiPaymentModel]), + returnValue: Future<_i4.TransactionDetailModel?>.value()) + as _i3.Future<_i4.TransactionDetailModel?>); +}