Unit tests run outside the app UI and do not affect frame rate or battery during normal app use. They help catch bugs early, improving app stability and user experience. Running many tests can consume CPU and memory during development but has no impact on the released app's runtime performance.
Unit testing with JUnit in Android Kotlin - Build, Publish & Deploy
Keep unit tests fast and isolated by mocking dependencies. Avoid long-running operations or network calls in tests. Use JUnit 5 features like @BeforeEach to set up test data efficiently. Run tests on the JVM instead of device/emulator to speed up feedback.
JUnit and test code are not included in the app bundle shipped to users, so they do not increase app size or startup time. Keep test dependencies in the testImplementation scope to avoid bloating the release APK or AAB.
Android uses JUnit for unit testing Kotlin/Java code on the JVM. iOS uses XCTest for Swift/Objective-C. Android unit tests run on the developer machine JVM, while iOS tests run in Xcode. Both platforms separate unit tests from UI tests to keep tests fast and reliable.
Unit tests do not affect app store submissions since test code is excluded from release builds. Ensure no test-only code or debug flags remain in production builds. Follow Google Play and Apple App Store guidelines to keep release APKs/AABs clean and signed properly.
Your app takes 5 seconds to load this screen. What's likely wrong?
- Heavy or blocking operations running on the main thread instead of background threads.
- Missing or slow unit tests causing delayed bug fixes and inefficient code.
- Large or unoptimized resources delaying startup.
Practice
Solution
Step 1: Understand unit test scope
Unit tests focus on small, isolated parts of code, like functions or classes.Step 2: Differentiate from other tests
UI tests and performance tests cover broader areas, not unit tests.Final Answer:
To check if a small part of code works correctly -> Option CQuick Check:
Unit test = small code check [OK]
- Confusing unit tests with UI or integration tests
- Thinking unit tests deploy or measure performance
- Assuming unit tests cover the whole app
Solution
Step 1: Identify JUnit test annotation
The@Testannotation marks a function as a test method in JUnit.Step 2: Understand other annotations
@Before runs setup code, @RunWith specifies runner, @Ignore skips tests.Final Answer:
@Test -> Option AQuick Check:
@Test marks test functions [OK]
- Using @Before instead of @Test for tests
- Confusing @RunWith with test marking
- Mistaking @Ignore as test annotation
import org.junit.Test
import kotlin.test.assertEquals
class CalculatorTest {
@Test
fun testAdd() {
val result = 2 + 3
assertEquals(5, result)
}
}Solution
Step 1: Analyze the test logic
The test adds 2 + 3 and stores in result, which equals 5.Step 2: Check assertion correctness
assertEquals expects 5 and result is 5, so assertion passes.Final Answer:
Test passes successfully -> Option AQuick Check:
2 + 3 = 5 matches assertEquals [OK]
- Assuming test fails when values match
- Thinking imports are missing when they are correct
- Confusing runtime errors with assertion failures
import org.junit.Test
import kotlin.test.assertEquals
class CalculatorTest {
@Test
fun testSubtract() {
val result = 5 - 2
assertEquals(4, result)
}
}Solution
Step 1: Calculate the actual result
5 - 2 equals 3, but expected value is 4 in assertEquals.Step 2: Check annotations and imports
@Test annotation and imports are correct; function name style is not enforced.Final Answer:
Wrong expected value in assertEquals -> Option DQuick Check:
5 - 2 = 3, expected 4 causes failure [OK]
- Ignoring mismatch between expected and actual values
- Thinking function names must start with 'test_'
- Assuming missing annotations when present
Solution
Step 1: Understand null checking assertions
assertNull explicitly checks if a value is null, making tests clear and readable.Step 2: Compare other options
assertEquals(null, ...) works but is less clear; assertTrue(...) works but less direct; assertNotNull is opposite.Final Answer:
assertNull(functionResult) -> Option BQuick Check:
Use assertNull to check null values [OK]
- Using assertNotNull when expecting null
- Using assertEquals(null, ...) instead of assertNull
- Using assertTrue with complex conditions unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
