0
0
Android Kotlinmobile~10 mins

Unit testing with JUnit in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A)
B]
C}
D;
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.
2fill in blank
medium

Complete 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'
AAssert
BTest
CAssertions
DRule
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.
3fill in blank
hard

Fix 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'
ATestCase
Btest
CTest
DTestFunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase @test which is not recognized by JUnit.
Using @TestCase or @TestFunction which do not exist.
4fill in blank
hard

Complete 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'
AEquals
BEqualsIgnoreCase
C)
D]
Attempts:
3 left
💡 Hint
Common Mistakes
Using assertEqualsIgnoreCase which does not exist in JUnit.
Closing the call with a bracket instead of a parenthesis.
5fill in blank
hard

Fill 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'
Acontains
B==
Ctrue
Dequals
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.