0
0
Android Kotlinmobile~20 mins

Unit testing with JUnit in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: Calculator Unit Test
This screen is for testing a simple Calculator class with JUnit unit tests in Kotlin. It checks basic math operations like addition and subtraction.
Target UI
---------------------
| Calculator Tests  |
|-------------------|
| Test Addition     |
| Test Subtraction  |
| Test Multiplication|
| Test Division     |
---------------------
Create a Calculator class with add, subtract, multiply, and divide methods.
Write JUnit test functions to verify each Calculator method returns correct results.
Use assertEquals to check expected vs actual values.
Include a test for division by zero that expects an exception.
Starter Code
Android Kotlin
package com.example.calculatortest

class Calculator {
    // TODO: Implement add, subtract, multiply, divide methods
}

// TODO: Create CalculatorTest class with JUnit tests
Task 1
Task 2
Task 3
Task 4
Task 5
Task 6
Task 7
Solution
Android Kotlin
package com.example.calculatortest

import org.junit.Test
import org.junit.Assert.*

class Calculator {
    fun add(a: Int, b: Int): Int = a + b
    fun subtract(a: Int, b: Int): Int = a - b
    fun multiply(a: Int, b: Int): Int = a * b
    fun divide(a: Int, b: Int): Int {
        if (b == 0) throw IllegalArgumentException("Cannot divide by zero")
        return a / b
    }
}

class CalculatorTest {
    private val calculator = Calculator()

    @Test
    fun testAdd() {
        assertEquals(5, calculator.add(2, 3))
    }

    @Test
    fun testSubtract() {
        assertEquals(1, calculator.subtract(3, 2))
    }

    @Test
    fun testMultiply() {
        assertEquals(6, calculator.multiply(2, 3))
    }

    @Test
    fun testDivide() {
        assertEquals(2, calculator.divide(6, 3))
    }

    @Test(expected = IllegalArgumentException::class)
    fun testDivideByZero() {
        calculator.divide(5, 0)
    }
}

We created a simple Calculator class with four methods for basic math operations. Each method returns the expected result.

The divide method checks if the divisor is zero and throws an IllegalArgumentException to prevent division by zero.

The CalculatorTest class uses JUnit 4 annotations. Each test method calls a Calculator method and uses assertEquals to compare the expected and actual results.

The testDivideByZero method expects an exception, verifying that dividing by zero is handled properly.

This setup helps ensure the Calculator works correctly and safely.

Final Result
Completed Screen
---------------------
| Calculator Tests  |
|-------------------|
| ✔ Test Addition    |
| ✔ Test Subtraction |
| ✔ Test Multiplication|
| ✔ Test Division    |
| ✔ Test Divide by 0 |
---------------------
Run tests using Android Studio or command line.
Each test shows a green checkmark if passed.
If a test fails, it shows a red cross and error details.
The divide by zero test confirms exception handling.
Stretch Goal
Add tests for floating point operations with Double numbers in Calculator.
💡 Hint
Create new methods for Double parameters and write tests using assertEquals with delta for precision.