Summary - Testing
- Kotlin course
- Intermediate
- Lesson 111/116
- 5 min
Combine @Test, assertions and MockK to write a calculator test.
Summary - Testing
In this group you learned the JUnit framework, the @Test annotation, the assertEquals/assertTrue assertions, the @BeforeEach/@AfterEach setup/teardown and mocking with MockK.
Now you will combine them: add both add and multiply to Calculator and test with two assertions, then mock a dependency with the fake service pattern. This structure is the foundation of writing reliable tests in Kotlin. Later, property-based testing and integration tests will build on it.
Practice Test add and multiply
Define class Calculator; inside it add fun add(a: Int, b: Int): Int = a + b and fun multiply(a: Int, b: Int): Int = a * b. Then define a helper fun assertEquals(expected: Int, actual: Int, label: String). In main create val c = Calculator(); call assertEquals(5, c.add(2, 3), "add") and assertEquals(12, c.multiply(3, 4), "multiply"); print println("all passed").
Starter code
class Calculator {
fun add(a: Int, b: Int): Int = a + b
fun multiply(a: Int, b: Int): Int = a * b
}
fun assertEquals(expected: Int, actual: Int, label: String) {
if (expected != actual) throw AssertionError("$label: expected $expected but was $actual")
}
// main: verify add(2,3) and multiply(3,4) results with assertEquals
// print "all passed"
fun main() {
// write here
}Sample solution
fun main() {
val c = Calculator()
assertEquals(5, c.add(2, 3), "add")
assertEquals(12, c.multiply(3, 4), "multiply")
println("all passed")
}Expected output
all passed
Practice The fake Greeter pattern
Define interface Greeter; inside it add fun greet(name: String): String. Define class FakeGreeter : Greeter whose greet body returns "Hello, $name!". Define class App(private val greeter: Greeter) whose fun run(name: String): String body returns greeter.greet(name). In main create val app = App(FakeGreeter()); print println(app.run("Alex")).
Starter code
interface Greeter {
fun greet(name: String): String
}
// FakeGreeter: implements Greeter as a fake, returns "Hello, {name}!"
// App: takes greeter, run(name) method returns greeter.greet(name)
// main: create App with FakeGreeter, print run for "Alex"
fun main() {
// write here
}Sample solution
interface Greeter {
fun greet(name: String): String
}
class FakeGreeter : Greeter {
override fun greet(name: String): String = "Hello, $name!"
}
class App(private val greeter: Greeter) {
fun run(name: String): String = greeter.greet(name)
}
fun main() {
val app = App(FakeGreeter())
println(app.run("Alex"))
}Expected output
Hello, Alex!
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is a test annotation?
- @Main
- @Test Correct answer
- @Class
- @Public
Explanation: @Test is JUnit's test method annotation.
2 Match the testing concepts
| Term | Meaning |
|---|---|
JUnit |
Test framework |
@Test |
Test method |
assertEquals |
Equality check |
MockK |
Mocking |