0
0
Kotlinprogramming~5 mins

Kotlin test assertions - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of assertEquals(expected, actual) in Kotlin tests?
It checks if the actual value is equal to the expected value. If not, the test fails.
Click to reveal answer
beginner
How does assertTrue(condition) work in Kotlin test assertions?
It verifies that the condition is true. If the condition is false, the test fails.
Click to reveal answer
beginner
What does assertNotNull(value) check in Kotlin tests?
It checks that the value is not null. If it is null, the test fails.
Click to reveal answer
intermediate
Explain the use of assertFailsWith<ExceptionType> { ... } in Kotlin tests.
It verifies that the code inside the block throws an exception of type ExceptionType. If no exception or a different type is thrown, the test fails.
Click to reveal answer
intermediate
What is the difference between assertEquals and assertSame in Kotlin?
assertEquals checks if two objects are equal in value, while assertSame checks if they are the exact same object (reference equality).
Click to reveal answer
Which assertion checks if a condition is true in Kotlin tests?
AassertTrue(condition)
BassertEquals(expected, actual)
CassertNotNull(value)
DassertFailsWith<Exception> { }
What happens if assertEquals(expected, actual) fails?
AThe test fails and reports a mismatch
BThe test passes silently
CThe test throws a runtime exception unrelated to assertion
DNothing happens
Which assertion would you use to check that a variable is not null?
AassertNull(value)
BassertTrue(value)
CassertNotNull(value)
DassertEquals(null, value)
How do you test that a block of code throws a specific exception in Kotlin?
AassertEquals<Exception> { }
BassertFailsWith<Exception> { }
CassertTrue { }
DassertNotNull { }
What does assertSame(obj1, obj2) check?
AIf obj1 and obj2 have the same value
BIf obj2 is not null
CIf obj1 is null
DIf obj1 and obj2 are the same object in memory
Describe how you would use Kotlin test assertions to verify that a function returns the expected value and does not return null.
Think about checking value equality and null separately.
You got /4 concepts.
    Explain how to test that a Kotlin function throws a specific exception when given invalid input.
    Focus on the assertion that expects an exception.
    You got /3 concepts.