android-kotlin
/

Android Testing – Unit, Integration, and UI Tests

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

android-kotlin

Android Testing – Unit, Integration, and UI Tests

The Testing Pyramid

An effective testing strategy for an Architect follows the 'Testing Pyramid' model. For Revochamp, this means having a massive base of fast-running Unit Tests, a middle layer of Integration Tests (to verify Method Channel data flow), and a thin top layer of end-to-end UI tests.

  1. Local Unit Tests (test/)

These tests run on your development machine's JVM and do not require an Android device or emulator. They are extremely fast. As a Lead Developer, you use these to verify your ViewModel logic and AI data parsing using MockK to simulate dependencies like Repositories or APIs.

KOTLINRead-only
1
// Testing RevoViewModel using JUnit 5 and MockK
class RevoViewModelTest {
    private val repository = mockk<AiRepository>()
    private lateinit var viewModel: RevoViewModel

    @Test
    fun `when generate is called, uiState should transition to loading`() = runTest {
        coEvery { repository.generate(any()) } returns "Success"
        viewModel = RevoViewModel(repository)

        viewModel.generateWidget("Button")

        assertEquals(UiState.Loading, viewModel.uiState.value)
    }
}

  1. Instrumented Tests (androidTest/)

These tests run on a real device or emulator. They are necessary when your code interacts with the Android framework (e.g., Room Database, SharedPreferences, or Context). In 2026, Hilt is used to inject test doubles into these instrumented environments effortlessly.

  1. UI Testing: Espresso vs. Compose Test

For legacy XML views, we use Espresso. For modern native modules in Revochamp, we use the Compose Testing library. Unlike Espresso, which uses 'View IDs', Compose testing uses 'Semantics' to find elements, making tests more robust and accessible.

KOTLINRead-only
1
// UI Test for a Compose-based AI Preview
@get:Rule
val composeTestRule = createComposeRule()

@Test
fun previewScreen_showsGeneratedTitle() {
    composeTestRule.setContent {
        RevoPreviewScreen(title = "AI Generated UI")
    }

    composeTestRule.onNodeWithText("AI Generated UI").assertIsDisplayed()
}

Testing Comparison: Native vs. Flutter

FeatureAndroid NativeFlutter
Unit FrameworkJUnit 5test package
Mocking LibraryMockK / MockitoMockito / Mocktail
UI TestingEspresso / Compose TestWidgetTester / Patrol
Execution PathJVM (Unit) / Dalvik (UI)Dart VM / Flutter Engine
Async TestingCoroutines (runTest)Future (await)
IsolationHilt / KoinProvider / Riverpod Overrides

Test Your Knowledge

Q1
of 3

Which testing library is the standard for modern declarative UI in 2026?

A
Espresso
B
Robolectric
C
Compose Testing Library
D
Appium
Q2
of 3

What is the primary benefit of a 'Local Unit Test'?

A
It can test hardware like the camera
B
It runs extremely fast on the JVM without an emulator
C
It tests the AndroidManifest.xml
D
It provides a screen recording of the test
Q3
of 3

Which annotation is used in MockK to mock a suspending function?

A
every
B
coEvery
C
suspendEvery
D
mockEvery

Frequently Asked Questions

What is the difference between 'test' and 'androidTest' folders?

The 'test' folder is for local JVM unit tests (no device needed). The 'androidTest' folder is for instrumented tests that require an Android environment to run. Generally, business logic goes in 'test', and UI/Database logic goes in 'androidTest'.

Why use MockK instead of Mockito for Kotlin?

MockK is built specifically for Kotlin. It has first-class support for final classes (which all Kotlin classes are by default), extension functions, and Coroutines (coEvery), which are difficult to handle with Mockito.

How do I test Method Channels?

To test a Method Channel, you should create an integration test that uses 'MethodChannel.setMockMethodCallHandler' on the Flutter side or 'MethodCallHandler' mocks on the Android side to verify that the correct data is serialized and sent across the bridge.

Previous

android authentication

Next

android debugging

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.