Understanding the Basics of Mockito in Flutter(Part1)
Esmaeil Ahmadipour
Posted on March 8, 2023
#Introduction to Mockito in Flutter
Mockito is a powerful mocking framework for unit testing in Flutter, allowing developers to create mock objects that simulate the behavior of real objects in controlled ways.
This helps developers to test the functionality of their code without having to rely on external dependencies. It also helps to identify bugs and errors in the code, as it isolates the code under test and verifies its behavior.
With Mockito, developers can create mocks of classes, interfaces, and even functions, allowing them to test their code in isolation and ensure that their code is working as expected.
#Setting up Mockito in flutter
Mockito is a popular mocking framework for unit testing in Java. It is now available for Flutter developers as well, thanks to the mocktail package. Setting up Mockito in Flutter and using the mocktail package is easy and straightforward.
The mocktail package provides a Mock class that can be used to create a mock object of any class. The mock object can then be used to define the behavior of the class and write tests.
In the example above, we have created a Calculator class with an add() method that takes two numbers as parameters and returns their sum. To test this method, we have created a mock object of the Calculator class using the Mock class provided by the mocktail package. We have then defined the behavior of the mock object using the when() method. Finally, we have written a test to verify that the mock object returns the expected result.
/// Full Code ;)
import 'package:test/test.dart';
import 'package:mocktail/mocktail.dart';
class MockCalculator extends Mock implements Calculator {}
void main() {
test('add() should return the sum of two numbers', () {
// This is a test to verify the behavior of the add() method of the Calculator class.
// First, a mock object of the Calculator class is created.
var calculator = MockCalculator();
// The behavior of the mock object is then defined.
// When the add() method is called with 1 and 2 as parameters, it should return 3.
when(() =>calculator.add(1, 2)).thenReturn(3);
// The test is written to call the add() method with 1 and 2 as parameters.
int result = calculator.add(1, 2);
// Finally, the result is verified to be 3, which is the expected result.
expect(result, 3);
});
}
class Calculator {
int add(int a, int b) {
return a + b;
}
}
#Writing Unit Tests with Mockito in flutter
Writing unit tests with Mockito in Flutter is a great way to ensure that your code is working as expected. Mockito is a mocking framework that allows you to create mock objects and stub methods for testing. With Mockito, you can easily create mocks of classes and methods, and then use them to test your code.
In this section , we’ll be looking at how to write unit tests with Mockito in Flutter. We’ll be using the MyClass class from the example above as our example class. We’ll be writing tests for the setData() and getData() methods of MyClass.
First, we’ll need to import the Mocktail library and the test package. Then, we’ll create a MyClassMock class that extends the Mock class. This will allow us to create mock objects of our MyClass class.
Next, we’ll create a setUp() method that will create an instance of MyClassMock and MyClass. This will allow us to use these instances in our tests.
Then, we’ll create a group() method that will group our tests together. This will make it easier to read our tests and understand what they are testing.
Finally, we’ll write three test() methods that will test the setData() and getData() methods of MyClass, as well as the getData() method of MyClassMock with a default value.
Once we have written our tests, we can run them to make sure that our code is working as expected. If any of the tests fail, we can go back and debug our code until all of the tests pass.
/// Full Code ;)
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
class MyClassMock extends Mock implements MyClass {}
// This test suite is used to test the functionality of the MyClass class.
void main() {
late MyClassMock myClassMock;
late MyClass myClass;
// The setUp() method is used to create an instance of MyClassMock and MyClass.
setUp(() {
myClassMock = MyClassMock();
myClass = MyClass();
});
// The group() method is used to group the tests together.
group('do setData and getData succeed', () {
// The first test() method is used to test the setData() and getData() methods of MyClass.
test('setData', () {
myClass.setData("test");
expect(myClass.getData(), equals("test"));
});
// The second test() method is used to test the getData() method of MyClassMock.
test('getData', () {
when(() => myClassMock.getData()).thenReturn("TEST");
expect(myClassMock.getData(), equals("TEST"));
});
// The third test() method is used to test the getData() method of MyClassMock with a default value.
test('getData with default value', () {
expect(myClassMock.getData(), equals(null));
});
});
}
class MyClass {
String? _data;
String? getData() {
return _data;
}
void setData(String data) {
_data = data;
}
}
Posted on March 8, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.