Testing and builds on Android: JUnit, Robolectric, Gradle and CI
You manage Gradle scripts and dependencies, test Kotlin and Java code with JUnit and Robolectric without an emulator, set quality gates with lint and R8 and tie it all into a CI pipeline.
Level 201 · 22 lessons · 18 practice labs
Lessons
Gradle in depth
- Build scripts: the Kotlin DSL and version catalogs
- Dependencies: configurations and transitive dependencies
- Dependency verification: checksums and lock files
- BuildConfig, local properties and keeping secrets out of the repository
- Short task: find the transitive library with a known flaw in the dependency tree and upgrade it
Unit tests
- Test structure with JUnit: arrange, act, assert
- Test doubles: MockK in Kotlin, Mockito in Java
- Robolectric: testing Android classes without an emulator
- Testing coroutines and flows
- Testing the data layer: Room and a mock web server
- Short task: write an eight-test suite for the login screen logic
Quality gates
- Lint: baseline, severities and custom rules
- Code quality with Detekt and ktlint
- Measuring test coverage
- R8: shrinking, obfuscation and keep rules
- Short task: set up a quality gate that fails the build on warnings
Continuous integration
- A CI pipeline for Android: build, test, check
- Release signing in CI and protecting the key
- Reproducible builds and artefact verification
- Dependency updates and the software bill of materials (SBOM)
- Short task: find the signing password leaking in a CI script and fix it
Capstone
- Capstone: wire an app into a pipeline that runs tests, lint and a signed release in one command
Practice labs
- Print the dependency tree and locate a library: Produce the dependency tree with Gradle and find which version of the given library comes in.
- Upgrade a transitive library with a known flaw: Find the dependency that brings in the vulnerable version, force the upgrade and run the tests.
- Turn on dependency verification and catch a tampered artefact: Enable checksum verification, show that an altered library stops the build and correct the record.
- Find the API key committed to the repository: Find the key written in plain text in the project files and report where it is.
- Move the key to a local properties file: Feed the key into BuildConfig from a file outside version control and confirm the build.
- Wire secret scanning into the build: Add a Gradle task that fails the build when a key pattern is found in the repository and test it with two samples.
- Write an arrange-act-assert test for a class: Write three tests for a class that totals a basket.
- Test the network layer with a test double: Mock the login repository with MockK and test successful and failed login cases.
- Write an eight-test suite for login logic: Write a suite covering the lockout counter, empty fields and token storage behaviour and make it all pass.
- Test a preferences class with Robolectric: Verify a class that reads settings with a test that runs on the JVM.
- Test a Room DAO with an in-memory database: Test insert, update and delete queries on an in-memory database.
- Test error cases with a mock web server: Write tests showing that on a 401 response the token is cleared and re-login is requested.
- Run lint and read the report: Run lint on the project and list the error-level findings.
- Set up a gate that fails the build on warnings: Configure lint and Detekt to treat warnings as errors and create a baseline for existing findings.
- Narrow the R8 keep rules: Reduce a rule that keeps the whole package to just the needed classes and show the release build passes its tests.
- Order the steps in a pipeline definition: Find the order of build, test and check steps in the given CI file and the step that is missing.
- Fix the signing password leaking in a CI script: Find the signing password echoed to the log, move it to a secret variable and confirm it is masked in the log.
- Verify a reproducible build: Build twice from the same source, compare the output hashes and find and remove the source of the difference.
About this course
You manage Gradle scripts and dependencies, test Kotlin and Java code with JUnit and Robolectric without an emulator, set quality gates with lint and R8 and tie it all into a CI pipeline.
What you will learn
- Gradle in depth
- Unit tests
- Quality gates
- Continuous integration
- Capstone
Prerequisites
Finishing the level 101 courses of the same path, or knowing those topics, is enough.
Lab environment
Lessons run in your own Kali Linux terminal that opens in the browser. Nothing to install; the lab starts clean in every lesson.
Frequently asked questions
- Is this course right for my level?
- The course is at level 201; it assumes you know the topics of the lower-level courses.
- Is there anything to install?
- No. Every lesson runs in your own lab environment that opens in the browser.
- How do lessons work?
- Each lesson has 8 to 10 steps and a short quiz. Early steps show the command to type, later steps give less help; if two attempts fail, the solution opens.
- Can I try it for free?
- Yes. The first three lessons of every course are free.