Skip to content

Commit

Permalink
adding test cases with mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
GJJ2019 committed Jul 11, 2022
1 parent 823f3c0 commit 2ba8610
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 16 deletions.
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
80 changes: 64 additions & 16 deletions test/easy_upi_payment_test.dart
Original file line number Diff line number Diff line change
@@ -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<TransactionDetailModel?> 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<MethodChannelEasyUpiPayment>());
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<EasyUpiPaymentException>()),
);
});

test('startPayment with failed Exception', () async {
when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow(
EasyUpiPaymentException.fromException(
PlatformException(
code: EasyUpiPaymentExceptionType.failedException.toString(),
),
),
);
expect(
() => mockPlatform.startPayment(fakeEasyUpiPaymentModel),
throwsA(isA<EasyUpiPaymentException>()),
);
});

test('startPayment with submitted Exception', () async {
when(mockPlatform.startPayment(fakeEasyUpiPaymentModel)).thenThrow(
EasyUpiPaymentException.fromException(
PlatformException(
code: EasyUpiPaymentExceptionType.submittedException.toString(),
),
),
);
expect(
() => mockPlatform.startPayment(fakeEasyUpiPaymentModel),
throwsA(isA<EasyUpiPaymentException>()),
);
});

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<EasyUpiPaymentException>()),
);
});
}
40 changes: 40 additions & 0 deletions test/easy_upi_payment_test.mocks.dart
Original file line number Diff line number Diff line change
@@ -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?>);
}

0 comments on commit 2ba8610

Please sign in to comment.