Reputation: 2182
I have started to write tests. I saw mockito
and mocktail
as the most used testing libraries. I couldn't find any question/articles that explains differences between them. If there is a experienced developers -who used both of them- can you explain differences/advantages/disadvantages of them? Which one should I prefer?
Upvotes: 39
Views: 10114
Reputation: 2182
Answering my own question after experiencing both of them:
At first I decided to use mockito since it has more reputation. But, it was difficult to regenerate mock classes again and again. But then, I wanted to give a chance to mocktail and definitely saw that it is better! I would recommend using that instead of mockito for a few advantages. Being not have to generate mock classes is NOT the only advantage!
Upvotes: 26
Reputation: 872
1. Assuming that you are new with Flutter, it would be probably easier for you to utilize the mocktail package.
The main "inconvenience" with the mockito package is that you need to generate the mocks running flutter pub run build_runner build, define meta-annotations like @GenerateMocks, and imports like xxx.mocks.dart, and an extra build_runner dev dependency at your pubspec.yaml.
The mocktail package simplifies mocking: you just need to extend the Mock class. That's it. Without code-generating, annotations, "magic" xxx.mocks.dart imports.
2. Also, you have to keep in mind that the mocktail package is very new and has a stable history of just 10 months. The mockito package is a proven by time and developers library that has almost 8 years of history of stable releases: the library is well-known and is widespread among the Flutter and Dart community.
With the experience, you will better understand which library better fits your projects' needs.
PS: you can take a look at the code snippets of both packages.
The mocktail snippet:
import 'package:mocktail/mocktail.dart';
// Real Cat class
class Cat {
// ...
}
// Mock Cat Class
class MockCat extends Mock implements Cat{}
void main() {
// Create a Mock Cat instance
final cat = MockCat();
}
The mockito snippet screenshot
import 'package:mockito/annotations.dart'
import 'package:mockito/mockito.dart'
import 'cat.mocks.dart';
// REAL class
class Cat {
// ...
}
// Annotation which generates the cat.mocks.dart library
@GenerateMocks([Cat])
void main() {
// Create a mock object
final cat = MockCat();
}
Upvotes: 61