Better Android Testing at Airbnb (Part 4: Testing ViewModels)
Article Summary
Airbnb built a testing framework so elegant that their ViewModels tests read like plain English. Here's how they made testing actually enjoyable.
Eli Hart from Airbnb Engineering shares their custom unit testing framework for Android ViewModels. The approach transforms verbose test code into a clean DSL that prioritizes developer experience without sacrificing flexibility.
Key Takeaways
- Tests follow one rule: single function call with state input produces state output
- Simple syntax: setText test is just 3 lines with withParams and expectState
- Network assertions built in: validate API calls and response handling together
- Auto-generated test cases for basic setters test multiple values automatically
- IntelliJ plugin generates test scaffolding alongside new ViewModels
Airbnb reduced testing friction across hundreds of modules with a DSL that makes ViewModel tests readable and maintainable.
About This Article
Eli Hart's team had to test ViewModels across hundreds of Android modules. They needed to keep their approach consistent when verifying state changes and dependency calls, but doing so meant writing a lot of boilerplate code.
Airbnb built a DSL framework that could be extended for different needs. To test ViewModel functions, you pass in initial state and parameters, then check whether the state changed or verify dependency calls using Mockito. The framework also handles nested state changes and validates network requests.
The framework made it possible to automatically generate test scaffolding. An IntelliJ IDEA plugin was created that generates test files whenever a new ViewModel is added. This cut down setup time significantly and made it easier for teams across the organization to adopt testing in their large codebase.