Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a basic JUnit test function in Kotlin.
Android Kotlin
import org.junit.Test import org.junit.Assert.* class CalculatorTest { @Test fun testAddition() { val result = 2 + 3 assertEquals(5, result[1]) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a bracket or brace instead of a parenthesis to close the function call.
Adding a semicolon which is not needed in Kotlin.
✗ Incorrect
The assertEquals function call must be properly closed with a parenthesis ")".
2fill in blank
mediumComplete the code to import the JUnit Assert class correctly.
Android Kotlin
import org.junit.[1].*
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Test instead of Assert, which only provides the @Test annotation.
Using Assertions which is from JUnit 5, but here we use JUnit 4 style.
✗ Incorrect
The Assert class contains the assertion methods like assertEquals used in tests.
3fill in blank
hardFix the error in the test function annotation to make it a valid JUnit test.
Android Kotlin
class SampleTest { @[1] fun sampleTest() { assertTrue(true) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase @test which is not recognized by JUnit.
Using @TestCase or @TestFunction which do not exist.
✗ Incorrect
The correct annotation to mark a function as a JUnit test is @Test with capital T.
4fill in blank
hardComplete the code to write an assertion that checks if two strings are equal.
Android Kotlin
class StringTest { @Test fun testStrings() { val expected = "hello" val actual = "hello" assert[1](expected, actual) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertEqualsIgnoreCase which does not exist in JUnit.
Closing the call with a bracket instead of a parenthesis.
✗ Incorrect
The assertion method is assertEquals and it must be closed with a parenthesis.
5fill in blank
hardFill all three blanks to write a test that checks if a list contains a specific element.
Android Kotlin
class ListTest { @Test fun testContains() { val items = listOf("apple", "banana", "cherry") assertTrue(items.[1]("banana") [2] [3]) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using equals instead of contains to check list membership.
Not comparing the result to true, which is needed for assertTrue.
✗ Incorrect
The list method contains checks if an element is present, and assertTrue expects a boolean expression, so we check if items.contains("banana") == true.