0
0
Kotlinprogramming~15 mins

Kotlin test assertions - Mini Project: Build & Apply

Choose your learning style9 modes available
Kotlin test assertions
📖 Scenario: You are writing simple tests to check if your Kotlin functions work correctly. Testing helps you catch mistakes early, like checking if a calculator gives the right answer.
🎯 Goal: Build a Kotlin program that uses test assertions to verify the results of simple functions.
📋 What You'll Learn
Create a function that returns the sum of two numbers
Create a function that returns the product of two numbers
Write assertions to check if these functions return expected results
Print a message if all tests pass
💡 Why This Matters
🌍 Real World
Testing small functions helps ensure your code works correctly before using it in bigger programs.
💼 Career
Writing and understanding test assertions is a key skill for software developers to maintain code quality.
Progress0 / 4 steps
1
Create sum and product functions
Write a Kotlin function called sum that takes two Int parameters named a and b and returns their sum. Also write a function called product that takes two Int parameters named a and b and returns their product.
Kotlin
Need a hint?

Define two functions with the exact names sum and product. Each should take two integers and return an integer.

2
Set expected test results
Create two val variables named expectedSum and expectedProduct. Set expectedSum to 7 and expectedProduct to 12.
Kotlin
Need a hint?

Use val to create constants for the expected results.

3
Write assertions to test functions
Write two assert statements. The first should check if sum(3, 4) equals expectedSum. The second should check if product(3, 4) equals expectedProduct.
Kotlin
Need a hint?

Use assert(condition) to check if the function results match the expected values.

4
Print success message
Write a println statement that prints "All tests passed!".
Kotlin
Need a hint?

Use println("All tests passed!") to show the success message.