[kotlin] JUnit 테스트 케이스에서 배열과 컬렉션 테스트
JUnit은 Java 및 Kotlin 프로젝트에서 단위 테스트를 작성할 수 있는 유용한 도구입니다. 이를 사용하면 배열과 컬렉션과 같은 다양한 데이터 구조를 손쉽게 테스트할 수 있습니다.
배열 테스트
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class ArrayTest {
@Test
fun testArray() {
val expectedArray = arrayOf(1, 2, 3, 4, 5)
val actualArray = arrayOf(1, 2, 3, 4, 5)
Assertions.assertArrayEquals(expectedArray, actualArray)
}
}
위의 코드에서는 Assertions.assertArrayEquals
를 사용하여 예상 배열과 실제 배열이 일치하는지 테스트합니다.
컬렉션 테스트
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
class CollectionTest {
@Test
fun testList() {
val expectedList = listOf("apple", "banana", "orange")
val actualList = listOf("apple", "banana", "orange")
Assertions.assertEquals(expectedList, actualList)
}
@Test
fun testMap() {
val expectedMap = mapOf(1 to "one", 2 to "two", 3 to "three")
val actualMap = mapOf(1 to "one", 2 to "two", 3 to "three")
Assertions.assertEquals(expectedMap, actualMap)
}
}
위의 코드에서는 Assertions.assertEquals
를 사용하여 예상 컬렉션과 실제 컬렉션이 일치하는지 테스트합니다.
JUnit을 사용하여 배열과 컬렉션을 테스트하는 것은 간단하지만 매우 유용합니다. 이를 통해 프로젝트의 안정성을 높일 수 있습니다.